发送http请求不能写在activity的主线程里,需要另外开个线程来实现这个功能

解决方案 »

  1.   

    本来需要重新启动一个activity,不过你可以在super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);后面加入以下代码,就可以在一个activty访问网络了。 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()        
            .detectDiskReads()        
            .detectDiskWrites()        
            .detectNetwork()   // or .detectAll() for all detectable problems        
            .penaltyLog()        
            .build());        
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()        
            .detectLeakedSqlLiteObjects()     
            .penaltyLog()        
            .penaltyDeath()        
            .build()); 
      

  2.   

    public class MainActivity extends Activity {
    private Thread mThread;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     mThread = new Thread(runnable);  
             mThread.start();//线程启动  
    }
     Runnable runnable = new Runnable() {  
             
            @Override  
            public void run() {//run()在新的线程中运行  
                HttpClient hc = new DefaultHttpClient();  
                HttpPost post=new HttpPost("http://carebaby.duapp.com/system/receivemessage.php");
    List<NameValuePair>params=new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("username", "username"));
                    params.add(new BasicNameValuePair("y", "x"));
                    params.add(new BasicNameValuePair("x", "y"));
                    try {  
                        HttpResponse hr = hc.execute(post);                   
                        Toast.makeText(getApplicationContext(), hr.getEntity().getContent().toString(),
                              Toast.LENGTH_SHORT).show();
                    }
                    catch(Exception ex){
                       Log.i("http", "1"+ex.getMessage());
                    }
            }
          
     };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }}像这样开一个线程怎么还是不行呐,
      

  3.   

    你在manifest里面声明了internet权限了么?
    另外,你的代码里面,你的params并没有添加到post请求对象里面。
      

  4.   


    第一,你的List<NameValuePair>params根本就没有使用啊,应该在HttpResponse hr = hc.execute(post);  之前加上这两句:httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
    httpPost.setEntity(httpEntity);
    第二,不能在新线程中直接Toast的,得使用Handler将数据发回到主线程。
    第三,建议楼主将target改为17以上,这样当你在主线程中直接访问网络的时候会直接报NetworkInMainThreadException。
      

  5.   

    tryRunnable runnable = new Runnable() { @Override
    public void run() {// run()在新的线程中运行
    HttpClient hc = new DefaultHttpClient();
    HttpPost post = new HttpPost(
    "http://carebaby.duapp.com/system/receivemessage.php");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("username", "username"));
    params.add(new BasicNameValuePair("y", "x"));
    params.add(new BasicNameValuePair("x", "y"));
    try {
    post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse hr = hc.execute(post);
    InputStream is = hr.getEntity().getContent();
    StringBuilder sb = new StringBuilder();
    BufferedReader buffer = new BufferedReader(
    new InputStreamReader(is));
    String line = null;
    while ((line = buffer.readLine()) != null)
    sb.append(line);
    Looper.prepare();
    Toast.makeText(getApplicationContext(), sb.toString(),
    Toast.LENGTH_SHORT).show();
    Looper.loop();
    } catch (Exception ex) {
    Log.i("http", "1" + ex.getMessage());
    }
    } };