我收到错误:android.os.NetworkOnMainThreadException。我的应用在模拟器上运行良好。然后我在某些设备上检查它,它在 android 版本 2.3.5 上运行良好,但在第二个版本为 4.0.3 的设备上出现错误。我正在使用 AsyncTask 如下:
private class TheTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(Main.this, "",
"Loading. Please wait...", true);
}
@Override
protected String doInBackground(String... params) {
request = new SoapObject(NAMESPACE, METHOD_NAME);
userid = new PropertyInfo();
userid.setName("UserId");
userid.setValue(UserId);
userid.setType(String.class);
request.addProperty(userid);
SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envp.dotNet = true;
envp.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envp);
SoapPrimitive response = (SoapPrimitive) envp.getResponse();
Response = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
return Response;
}
@Override
protected void onPostExecute(String result) {
if (dialog != null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
try {
splitData = result.split("\\;");
username.setText(splitData[0]);
URL newurl = new URL("" + WebsiteURL + "/images/profileImg/"
+ splitData[1] + "");
Bitmap mIcon_val = BitmapFactory.decodeStream(newurl
.openConnection().getInputStream());
userphoto.setImageBitmap(mIcon_val);
} catch (Exception e) {
username.setText(e.toString());
}
}
}