0
public class NineJan2012Activity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = null;
InputStream is = null;
StringBuilder sb=null;
String result1=null;
int lineno=0;
//http post
// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
// StrictMode.setThreadPolicy(policy);
try{
HttpClient httpclient = new DefaultHttpClient();
lineno++;
HttpPost httppost = new HttpPost("http://10.0.2.2/food.php");
lineno++;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(0);
//nameValuePairs.add(new BasicNameValuePair("id", "12345"));
//nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
lineno++;
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
lineno++;
HttpResponse response = httpclient.execute(httppost);
lineno++;
HttpEntity entity = response.getEntity();
lineno++;
is = entity.getContent();
lineno++;
Log.e("log_tag", "Success in http connection"+lineno);
}
catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString()+lineno);
}
}
}
在這段代碼中我有線上的錯誤HttpResponse response = httpclient.execute(httppost);
Android網絡服務
android.os.NetworkOnMianThreadException
可能是什麼原因?