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();
}
}
}
aneurinc :
You could pass the text value of the views into the constructor of the async task and access them via the inner reference.\n\nclass RetrieveFeedTask extends AsyncTask<String, String, String> {\n\n private String emailText, passwordText;\n\n RetrieveFeedTask(String emailText, String passwordText) {\n this.emailText = emailText;\n this.passwordText = passwordText;\n }\n\n protected String doInBackground(String... urls) {\n ...\n }\n}\n\n\nYou would then call it like so:\n\n new RetrieveFeedTask(\n emailText.getText().toString(), \n passwordText.getText().toString()\n ).execute();\n\n\nAlternatively, you could pass parameters directly to doInBackground() as an array.\n\nprotected Void doInBackground(String... urls) {\n String email = urls[0]; \n String password = urls[1]; \n .... \n}\n\n\nAnd then call it like so:\n\nString[] urls = { \n emailText.getText().toString(), \n passwordText.getText().toString() \n};\n\nnew RetrieveFeedTask().execute(urls);\n",
2017-07-07T15:54:44
instanceof :
You could use this (AsyncTask with onPreExecute and onPostExecute, but without NetworkOnMainThreadException): \n\nclass RetrieveFeedTask extends AsyncTask<String, String, String> {\n\n @Override\n protected void onPreExecute() {\n super.onPreExecute();\n }\n\n @Override\n protected String doInBackground(String... urls) {\n\n String result = \"\";\n\n SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);\n\n request.addProperty(\"userName\", emailText.getText().toString());\n request.addProperty(\"userPassword\", passwordText.getText().toString());\n\n SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);\n\n envelope.setOutputSoapObject(request);\n envelope.dotNet = true;\n\n try {\n HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);\n\n androidHttpTransport.call(SOAP_ACTION, envelope);\n\n SoapObject result = (SoapObject) envelope.bodyIn;\n if (result != null) {\n result = \"Login Successful!\";\n } else {\n result = \"Login Failed!\";\n }\n } catch (Exception e) {\n e.printStackTrace();\n }\n return result;\n }\n\n @Override\n protected String onPostExecute(String result) {\n super.onPostExecute(result);\n\n if (result.equals(\"Login Successful!\")) {\n Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();\n } else {\n Toast.makeText(getApplicationContext(), \"Login Failed!\", Toast.LENGTH_LONG).show();\n }\n }\n}\n",
2017-07-07T18:47:33