此问题在此处已有答案:
How can I fix 'android.os.NetworkOnMainThreadException'?(66个答案)
9年前关闭了。
我在Web上成功部署了我的Web服务。我可以从我的应用程序中的Android 2.3调用它。但它无法从我的应用程序中调用Web服务,这是在Android 4.0上运行,它给出了空错误。我使用的最低SDK到API 10
我调用Web服务的代码如下
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("number", username.getText().toString());
request.addProperty("password", password.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
Toast.makeText(getBaseContext(), "Wait while connecting server",Toast.LENGTH_LONG).show();
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
txt.setText("upto result");
if (result != null) {
String h = String.valueOf(result.getProperty(0));
txt.setText(h);
if (h.equalsIgnoreCase("true")) {
Intent myIntent = new Intent(getBaseContext(),
ContactList.class);
myIntent.putExtra("mobileNumber", username
.getText().toString());
startActivity(myIntent);
} else {
Toast.makeText(getBaseContext(), "Not register or invalid password",Toast.LENGTH_LONG).show();
txt.setText("Invalid Credential");
}
} else {
txt.setText("Error while connecting server");
}
} catch (Exception e) {
e.printStackTrace();
txt.setText("Check internet connection" + e.getMessage());
}
You Can Download my app from here
我的代码中有什么错误。
先谢了
1条答案
按热度按时间niknxzdl1#
这可能与API 11和更高版本中不允许在主线程中访问网络有关,您可能必须使用
AsyncTask
。把你的代码块嵌入到一个
AsyncTask
中,这样应该可以解决这个问题。