1

I can connect to a webservice but what I want to do is after connecting the webservice I want to open the gallery and choose a video from there. Could you please help me where is the problem? here is my code:

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
answer88
  • 165
  • 7
  • 19

2 Answers2

1

Maybe the Android class AsyncTask can be helpfull.

make the WebService request on doInBackground method and show the gallery on postExecute method.

more info: AsyncTask on Android doc

Update: Replace the onclick method content with:

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
    };
};
sabadow
  • 5,095
  • 3
  • 34
  • 51
  • But I need to use AsnycTask while uploading too. Can I use this more than once in the app? – answer88 Jul 12 '12 at 12:55
  • what about the arguments? what should I use as arguments in this situation? – answer88 Jul 12 '12 at 12:58
  • In the example code of the question the Ws returns an Integer, so for example AsyncTask, the String are params to the doInBackground method and the Integer the response of this method and the param of the onPostExecute method. – sabadow Jul 12 '12 at 13:01
  • could you plesa show me in the code or corrected version? Because I am a bit confused. @sabadow – answer88 Jul 12 '12 at 13:08
  • Sorry but it does not connect anymore to the webservice too. @sabadow – answer88 Jul 12 '12 at 13:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13786/discussion-between-answer88-and-sabadow) – answer88 Jul 12 '12 at 13:32
0

here is the solution:

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.app.AlertDialog;
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);
    final  AlertDialog ad=new AlertDialog.Builder(this).create();
    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);
            Object response = null;
            try
            {
                 transport.call("http://tempuri.org/"+METHOD_NAME, envelope);
                 //getting the response from the webservice
                 response= envelope.getResponse();
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
            }//end of catch
            if(response!=null && Integer.parseInt(response.toString()) != 0)
            {
                openGaleryVideo();
            }
            else
            {
                ad.setMessage("Lütfen Kullanıcı Adınızı ve Şifrenizi Kontrol Ediniz.");
                ad.show();  
            }

        }//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
answer88
  • 165
  • 7
  • 19