android - 连接到网络服务后无法打开图库

标签 android web-services video gallery

我可以连接到网络服务,但我想做的是在连接网络服务后我想打开画廊并从那里选择一个视频。你能帮我看看问题出在哪里吗? 这是我的代码:

package com.isoft.uploader;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UploaderActivity extends Activity 
{

//ArrayList<Response> WebData= new ArrayList<Response>();
public static final int SELECT_VIDEO=1;
public static final String TAG="UploadActivity";
String path="";
final String NAMESPACE = "http://tempuri.org/";
final String SERVICEURL = "http://192.168.10.177/androidws/isandroidws.asmx";
final String METHOD_NAME1="OzelVeriAlanlariniGetir";
final String METHOD_NAME="KullaniciGiris";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button enter=(Button)findViewById(R.id.Enter);
    final EditText username=(EditText)findViewById(R.id.username);
    final EditText password=(EditText)findViewById(R.id.password);
    enter.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            //request code for Webservice
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //sending the username to the webservice
            request.addProperty("kullaniciAdi",username.getText().toString());
            //sending the password to the webservice
            request.addProperty("password",password.getText().toString());
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            //Putting the request in an envelope
            envelope.setOutputSoapObject(request);
            HttpTransportSE transport = new HttpTransportSE(SERVICEURL);
            try
            {
                 transport.call("http://tempuri.org/"+METHOD_NAME, envelope);
                 //getting the response from the webservice
                 Integer response = (Integer) envelope.getResponse();
                 if(response!=0)
                 {
                    openGaleryVideo();
                 }//end of if statement
            }//end of try 
            catch(Exception exception)
            {
                exception.printStackTrace();
            }//end of catch

        }//end of onClick method
    });//end of OnclickListener method
}//end of onCreate method


public void openGaleryVideo()
{
    Intent intent=new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Video"),SELECT_VIDEO);
}//end of openGaleryVideo method

public String getPath(Uri uri)
{   
    String[] projection = { MediaStore.Video.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}//end of getPath method
}//end of main

最佳答案

也许 Android 类 AsyncTask 会有所帮助。

在 doInBackground 方法上发出 WebService 请求并在 postExecute 方法上显示画廊。

更多信息:AsyncTask on Android doc

更新: 将 onclick 方法内容替换为:

AsyncTask<String, Void, Integer> task = new AsyncTask<String, Void, Integer>(){

    @Override
    protected Integer doInBackground(String... params) {


        // TODO Auto-generated method stub
        //request code for Webservice
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        //sending the username to the webservice
        request.addProperty("kullaniciAdi",username.getText().toString());
        //sending the password to the webservice
        request.addProperty("password",password.getText().toString());
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        //Putting the request in an envelope
        envelope.setOutputSoapObject(request);
        HttpTransportSE transport = new HttpTransportSE(SERVICEURL);
        try
        {
             transport.call("http://tempuri.org/"+METHOD_NAME, envelope);
             //getting the response from the webservice
             return (Integer) envelope.getResponse();
        }//end of try 
        catch(Exception exception)
        {
            exception.printStackTrace();
        }//end of catch
        return 0;
    }


    protected void onPostExecute(Integer result) {
        if(result!=0)
        {
           openGaleryVideo();
        }//end of if statement
    };
};

关于android - 连接到网络服务后无法打开图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452232/

相关文章:

java - 所有 java 类和对象都消失了(eclipse、android SDK)

c# - 有利于性能

JSON 响应模型设计问题

video - Vimeo 视频自动播放在 Safari 11 中不起作用 : NotAllowedError

java - 解锁手机后重新启动应用程序覆盖

java - Android Proguard - 一步一步

android - 如何在 SQLCipher 中加密/解密

web-services - 获取访问者的位置,是否使用 GPS(如果可用)?

audio - 如何使用具有多个音频流的 mpv 播放视频

ios - 如何使用 pjsip 2.5.5 或 2.6 在 Ios 中进行视频通话?