我在AsynkTask中编写了网络操作代码时得到了android.os.NetworkOnMainThreadException。抛出这个例外还有其他原因吗?
这是我的代码:
public class Background_confirmation extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog = ProgressDialog.show(Confirmation.this,
"Please wait...", "Retrieving data ...", true);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://68.121.167.160/sip_chat_api/create_account.php?useralias="
+ useralias + "&cntname=" + cntcode + "");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
if (backgroung_flag == 1) {
} else {
if (is != null) {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag",
"Error converting result " + e.toString());
}
}
}
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
if (progressDialog.isShowing()) {
progressDialog.dismiss();
// progressDialog.setCancelable(true);
}
super.onPostExecute(result);
}
}
我在OnCreate()
中调用此类new Background_confirmation().execute();
答案 0 :(得分:4)
您使用了错误的AsyncTask方法来放置与网络相关的代码。请将其移至doInBackground
,因为onPreExecute
发生在主线程上。所以,发生了异常。详细信息为here。
答案 1 :(得分:4)
public class Background_confirmation extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(Confirmation.this, "Please wait...", "Retrieving data ...", true);
}
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://68.121.167.160/sip_chat_api/create_account.php?useralias=" + useralias + "&cntname=" + cntcode + "");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
if (backgroung_flag == 1) {
} else {
if (is != null) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
您的代码应该像上面那样改变。你需要考虑的事情
doInBackground()
doInBackground()
的结果,则必须在onPostExecute()
doInBackground()
中返回一个String值,其中AsyncTask
类的第三个参数也应该是String(这不是Wayne的答案)在您的代码中,除了“其他”部分外,您正在调用我们看不到的InputStream
。如果您仅使用InputStream
,请确保代码始终到达其他部分。
答案 2 :(得分:1)
将所有网络请求代码放在doInBackground
中。
onPreExecute
和onPostExecute
将在UI线程(主要主题)上运行,因此如果您使用这两种方法请求网络,您将获得一个例外。
public class Background_confirmation extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(Confirmation.this,
"Please wait...", "Retrieving data ...", true);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://68.121.167.160/sip_chat_api/create_account.php?useralias="
+ useralias + "&cntname=" + cntcode + "");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
if (backgroung_flag == 1) {
} else {
if (is != null) {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag",
"Error converting result " + e.toString());
}
}
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
if (progressDialog.isShowing()) {
progressDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}