I have the following problem. After I got the android.os.NetworkOnMainThreadException I went looking for a solution, and as it seems, AsyncTask is the best way to handle this.
But after I read several pages I still don't know how to implement AsyncTask.
First I will tell you what i know so far together with my questions:
Here I would try to call the webservice.
package net.frontend.androidapp.statusrequest;
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.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class PurchaseRequisitionRequest extends Activity {
private String METHOD_NAME = "parser" ;
private String NAMESPACE = "http://statusrequest.androidapp.webservice.backend.net";
private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private static final String URL = "http://10.35.105.31:8080/SAPInterfaceWebservice/services/XMLTransfromer?wsdl";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.purchase_requisition_request_activity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.purchase_requisition_request, menu);
return true;
}
public void checkStatus (View view) {
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
EditText edit = (EditText) findViewById (R.id.prRequest);
String s= edit.getText().toString();
long lineNr=Long.parseLong(s);
request.addProperty("lineNr", lineNr);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION,envelope);
Object result = envelope.getResponse();
String hString = result.toString();
String[] details = hString.split(",");
((TextView) findViewById (R.id.request_detail1)).setText("PR_Number: " +details[0].substring(1));
((TextView) findViewById (R.id.request_detail2)).setText("Result1: " +details[1]);
((TextView) findViewById (R.id.request_detail3)).setText("Result2: " +details[2]);
((TextView) findViewById (R.id.request_detail4)).setText("Result3: " +details[3]);
((TextView) findViewById (R.id.request_detail5)).setText("Entered Number: " + lineNr);
} catch (Exception E) {
E.printStackTrace();
((TextView) findViewById (R.id.request_detail1)).setText("ERROR: " + E.getClass().getName() + " : " + E.getMessage());
}
}
}
As far as I understand, the only thing I have to put here is
new MyClassName().execute(a, b, c);
right in my CheckStatus method. (This method is called, when a button is pressed)
So where does this line go?
private class MyClassName extends AsyncTask
I would now create a new class, give it a nice name and then put this line next
protected Long doInBackground(Params... params)
and then the code part of my CheckStatus .
Is this right so far?
The next thing is that I don't know, which parameters so you have to give the execute(a,b,c) call?
Can someone please give me some code example, using my code? I would really appreciate. I am sorry for asking so basic questions, but I don't understand how it works.
Thank you a lot for your help!