2012-01-10 169 views
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

可能是什么原因?

回答