gpt4 book ai didi

android - AsyncTask 错误 - android.os.NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-11-29 16:07:35 26 4
gpt4 key购买 nike

我收到错误: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());
}

}
}

最佳答案

你正在调用这段代码:

位图 mIcon_val = BitmapFactory.decodeStream(newurl
.openConnection().getInputStream());

在你的 onPostExecute 中。这意味着代码再次在主线程上执行,导致您的 NetworkOnMainThread 异常。在doInBackground中将图片保存到一个变量中,并在onPostCreate中将图片设置到ImageView中。

关于android - AsyncTask 错误 - android.os.NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16790527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com