0

I developed a web service from a dynamic web project using the web service wizard(followed a tutorial so I wasn't really sure what I was doing). The tutorial made me install the axis2 plugin so I'm assuming that's the one used for the web service generation. Anyhow, the service is working when I tried it from my browser so no problem in that part.

My problem is, I want to access this web service from an android application. I read a lot of tutorials past two days and tried to implement one, but later realized that it was outdated and it tried to connect to network in the main thread, which is not allowed after android 3.0. Then I found out about asynctask and tried my hand in it but there is just so much that I don't understand. The tutorials I found didn't explain any of those so I'm asking here.

The code I wrote before hearing about the asynctask is here:

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private final String NAMESPACE = "http://eko.erdem.com";
private final String URL = "http://159.20.89.38:9398/EkoBiletTest/services/EkoClass?wsdl";
private final String SOAP_ACTION = "http://eko.erdem.com/homePage";
private final String METHOD_NAME = "homePage";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    String place = "Paris";
    String checkinDate = "2013-06-10";
    String checkoutDate = "2013-06-12";

  //Pass value for place variable of the web service
    PropertyInfo placeProp =new PropertyInfo();
    placeProp.setName("place");//Define the variable name in the web service method
    placeProp.setValue(place);//Define value for place variable
    placeProp.setType(String.class);//Define the type of the variable
    request.addProperty(placeProp);//Pass properties to the variable

  //Pass value for checkinDate variable of the web service
    PropertyInfo checkinDateProp =new PropertyInfo();
    checkinDateProp.setName("checkinDate");
    checkinDateProp.setValue(checkinDate);
    checkinDateProp.setType(String.class);
    request.addProperty(checkinDateProp);


  //Pass value for checkinDate variable of the web service
    PropertyInfo checkoutDateProp =new PropertyInfo();
    checkoutDateProp.setName("checkoutDate");
    checkoutDateProp.setValue(checkoutDate);
    checkoutDateProp.setType(String.class);
    request.addProperty(checkoutDateProp);



    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();


        TextView tv = (TextView) findViewById(R.id.textView);
        tv.setText(response.toString());
        setContentView(tv);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Now I don't know which parts of the code should be in which function of the asynctask ( doinBackground() etc.) Also I don't understand the parameter sending system of asynctask. For example if I wanted to pass the NAMESPACE, URL, METHOD_NAME etc. to the asynctask how should I do it? Also where should I update the textView of the main activity? If someone can point me to a tutorial that covers these questions it would be great. I would also appreciate any answer of your own to those questions. Thanks in advance.

ManahManah
  • 263
  • 2
  • 5
  • 17

3 Answers3

3

Any network related work needs to be done in the doInBackground method of the AsyncTask

to send data to the asynctask you need to declare the incoming data for the class

puclic class MyNetowrkTask extends AsyncTask<String[],Void,Void>

the first parameter in the <> is the imcoming data, the second is the progress and the thirs is the result you return to the onPostExecute when the task is done.

so to pass strings in you would do something like this

new MyNetworkTask().execute(new String[] {"namespace","url","method"});

the in the doInBackground you get the data like this

protected Void doInBackground(String... params) {
    String[] data = params[0];
    String namespace = data[0];
    String url = data[1];
    String method = data[2];
    ....
}

Any update of a UI element needs to be done in the onPostExecute so if you want to update a textview it needs to be done there.

the code isnt exactly right but this will get you started to get the idea

tyczj
  • 71,600
  • 54
  • 194
  • 296
1

Now I don't know which parts of the code should be in which function of the asynctask ( doinBackground() etc.)

Basically, put the bulk of the work in doInBackground() and make sure not to try and update the UI from there. Any other of the methods can update the UI. Here is an answer that shows the basic structure.

Also I don't understand the parameter sending system of asynctask. For example if I wanted to pass the NAMESPACE, URL, METHOD_NAME etc. to the asynctask how should I do it?

You can pass those in the execute() function as vargsif you want those to be used in doInBackground() otherwise you can pass them in the constructor

MyTask task = new MyTask();  // pass variables here to go to the constructor`
task.execute()  // anything passed here will be received by doInBackground()`

Also where should I update the textView of the main activity?

This can be done in any methods besides doInBackground() depending on when you need to update it (before, during, or after) the task finishes

If this is an inner class of your Activity then you have the member variables of that Activity available to the task along with Context. If it is a separate file then you may need to pass Context to the constructor of your task.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

You should put the code which performs the HTTP request (probably whatever is inside your try-catch block) into the doInBackground() method of the async task. If you want to pass various parameters on to it you can encapsulate them into an object and pass the object. Alternatively you can pass them through a constructor to your async task and have them used in doInBackground().

Piovezan
  • 3,215
  • 1
  • 28
  • 45