アプリケーションを構築していて、AsyncTask の内部で NetworkOnMainThreadException を取得しています
電話:
new POST(this).execute("");
非同期タスク:
public class POST extends AsyncTask<String, Integer, HttpResponse>{
private MainActivity form;
public POST(MainActivity form){
this.form = form;
}
@Override
protected HttpResponse doInBackground(String... params) {
try {
HttpPost httppost = new HttpPost("http://diarwe.com:8080/account/login");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("email",((EditText)form.findViewById(R.id.in_email)).getText().toString()));
//add more...
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
return new DefaultHttpClient().execute(httppost);
} catch (Exception e) {
Log.e("BackgroundError", e.toString());
}
return null;
}
@Override
protected void onPostExecute(HttpResponse result) {
super.onPostExecute(result);
try {
Gson gSon = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
gSon.fromJson(IOUtils.toString(result.getEntity().getContent()), LogonInfo.class).fill(form);
} catch (Exception e) {
Log.e("BackgroundError", e.toString());
}
}
}
LogCat:
BackgroundError | android.os.NetworkOnMainThreadException
なぜこの例外が AsyncTask の doInBackground の do でスローされるのか、非常に混乱しています。