【发布时间】:2017-12-11 23:52:37
【问题描述】:
我是 SOAP webservices 和 AsyncTask 的新手,我正在开发一个使用 SOAP webservices 进行登录的应用程序。 我试图在 onCreate 方法中完成所有工作,但我得到了 android.os.NetworkOnMainThreadException。
所以我尝试使用 AsyncTask, 但我无法在 RetrieveFeedTask 类中获得对 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();
}
}
}
【问题讨论】:
-
这里似乎有一篇文章 -> helloandroid.com/tutorials/… 解释了如何做到这一点。但是,如果可能的话,我会研究 json 和 rest API,而不是 SOAP Webservices。
标签: android web-services soap android-asynctask android-studio-2.3