AsyncTask 和 SOAP 网络服务
AsyncTask and SOAP webservices
我是 SOAP 网络服务和 AsyncTask 的新手,我正在开发一个使用 SOAP 网络服务进行登录的应用程序。
我尝试在 onCreate 方法中完成所有工作,但我得到了 android.os.NetworkOnMainThreadException。
所以我尝试使用 AsyncTask,
但我无法在 RetrieveFeedTask class.
中获取对 emailText 和 passwordText 的引用
这是我的代码:
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();
}
}
}
您可以将视图的文本值传递给异步任务的构造函数并通过内部引用访问它们。
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) {
...
}
}
然后你可以这样称呼它:
new RetrieveFeedTask(
emailText.getText().toString(),
passwordText.getText().toString()
).execute();
或者,您可以将参数作为数组直接传递给 doInBackground()
。
protected Void doInBackground(String... urls) {
String email = urls[0];
String password = urls[1];
....
}
然后这样称呼它:
String[] urls = {
emailText.getText().toString(),
passwordText.getText().toString()
};
new RetrieveFeedTask().execute(urls);
您将 emailText 和 passwordText 直接传递到 AsyncTask 中,只需要做一些小的更改:
首先在您的 onClick 侦听器中:
字符串电子邮件 = emailText.getText().toString();
字符串密码=passwordText.getText().toString();
new RetrieveFeedTask().execute(email, password);
AsyncTask doInBackground 方法中的第二个:
字符串电子邮件 = urls[0];
字符串密码=urls[1];
几乎可以将任意值传递到 AsyncTask 的执行方法中,这些值作为参数接收到它的 doInBackground 方法中。
您可以使用这个(AsyncTask 与 onPreExecute 和 onPostExecute,但没有 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();
}
}
}
我是 SOAP 网络服务和 AsyncTask 的新手,我正在开发一个使用 SOAP 网络服务进行登录的应用程序。 我尝试在 onCreate 方法中完成所有工作,但我得到了 android.os.NetworkOnMainThreadException。
所以我尝试使用 AsyncTask, 但我无法在 RetrieveFeedTask class.
中获取对 emailText 和 passwordText 的引用这是我的代码:
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();
}
}
}
您可以将视图的文本值传递给异步任务的构造函数并通过内部引用访问它们。
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) {
...
}
}
然后你可以这样称呼它:
new RetrieveFeedTask(
emailText.getText().toString(),
passwordText.getText().toString()
).execute();
或者,您可以将参数作为数组直接传递给 doInBackground()
。
protected Void doInBackground(String... urls) {
String email = urls[0];
String password = urls[1];
....
}
然后这样称呼它:
String[] urls = {
emailText.getText().toString(),
passwordText.getText().toString()
};
new RetrieveFeedTask().execute(urls);
您将 emailText 和 passwordText 直接传递到 AsyncTask 中,只需要做一些小的更改:
首先在您的 onClick 侦听器中:
字符串电子邮件 = emailText.getText().toString();
字符串密码=passwordText.getText().toString();
new RetrieveFeedTask().execute(email, password);
AsyncTask doInBackground 方法中的第二个:
字符串电子邮件 = urls[0];
字符串密码=urls[1];
几乎可以将任意值传递到 AsyncTask 的执行方法中,这些值作为参数接收到它的 doInBackground 方法中。
您可以使用这个(AsyncTask 与 onPreExecute 和 onPostExecute,但没有 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();
}
}
}