Reputation: 39
I'm new to SOAP webservices and AsyncTask, I'm developing an app which is using SOAP webservices for login. I tried to do all work in onCreate method but I got android.os.NetworkOnMainThreadException.
So i tried to use AsyncTask, but I'm not able to get reference to emailText and passwordText in RetrieveFeedTask class.
Here is my code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static String SOAP_ACTION = "intentionally hided";
private static String NAMESPACE = "intentionally hided";
private static String METHOD_NAME = "intentionally hided";
private static String URL = "intentionally hided";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final EditText emailText = (EditText) findViewById(R.id.emailText);
final EditText passwordText = (EditText) findViewById(R.id.passwordText);
Button loginButton = (Button) findViewById(R.id.button_login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... urls) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userName", emailText.getText().toString());
request.addProperty("userPassword", passwordText.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 1063
Reputation: 1466
You could use this (AsyncTask with onPreExecute and onPostExecute, but without NetworkOnMainThreadException):
class RetrieveFeedTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
String result = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userName", emailText.getText().toString());
request.addProperty("userPassword", passwordText.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
result = "Login Successful!";
} else {
result = "Login Failed!";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected String onPostExecute(String result) {
super.onPostExecute(result);
if (result.equals("Login Successful!")) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
}
}
}
Upvotes: 1
Reputation: 1238
You could pass the text value of the views into the constructor of the async task and access them via the inner reference.
class RetrieveFeedTask extends AsyncTask<String, String, String> {
private String emailText, passwordText;
RetrieveFeedTask(String emailText, String passwordText) {
this.emailText = emailText;
this.passwordText = passwordText;
}
protected String doInBackground(String... urls) {
...
}
}
You would then call it like so:
new RetrieveFeedTask(
emailText.getText().toString(),
passwordText.getText().toString()
).execute();
Alternatively, you could pass parameters directly to doInBackground()
as an array.
protected Void doInBackground(String... urls) {
String email = urls[0];
String password = urls[1];
....
}
And then call it like so:
String[] urls = {
emailText.getText().toString(),
passwordText.getText().toString()
};
new RetrieveFeedTask().execute(urls);
Upvotes: 3
Reputation: 1
You pass emailText and passwordText directly into the AsyncTask, you just need to make a few minor changes:
First in your onClick Listener:
String email = emailText.getText().toString();
String password = passwordText.getText().toString();
new RetrieveFeedTask().execute(email, password);
Second in your AsyncTask doInBackground Method:
String email = urls[0];
String password = urls[1];
Pretty much you can pass arbitrary values into the execute method of AsyncTask which are received as parameters in it's doInBackground method.
Upvotes: 0