2012-10-30 214 views
0

我正在向服務器中的PHP站點發送HTTP請求。但是,當我發送請求時,電話卡住了幾秒鐘。有時它會花費很長時間來發送和接收請求的結果。在Android中發送HTTP請求

如何解決這個問題。請幫我找一個解決方案。謝謝。

這是我試過的代碼...

public void postData() { 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 

try { 
    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 

}

回答

0

使用AsyncTask從UI線程進行HTTP請求。更改您的代碼爲:

private class LongOperation extends AsyncTask<String, Void, String> { 

      @Override 
      protected Void doInBackground(String... params) { 

       //Call your post method here 
        postData(); 

        return null; 
      }  

      @Override 
      protected void onPostExecute(String result) { 
       // get result after operation complete 
      } 

      @Override 
      protected void onPreExecute() { 
      } 

      @Override 
      protected void onProgressUpdate(Void... values) { 
      } 
    } 
0

嘗試這樣,你可以使用:HttpClient.exeucute(httpPost)並得到 的HttpResponse

這項工作對我來說完美。

DefaultHttpClient client = new DefaultHttpClient(); 

    HttpPost post = new HttpPost(url); 

    List<NameValuePair> params = new ArrayList<NameValuePair>(); 

    pairs.add(new BasicNameValuePair("email", stringeditemail)); 

    UrlEncodedFormEntity formEntity = null; 
    try { 
     formEntity = new UrlEncodedFormEntity(params); 

    } catch (UnsupportedEncodingException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    post.setEntity(formEntity); 

    try { 

     HttpResponse response = client.execute(post); 
     int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode == HttpStatus.SC_OK) { 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 
      return iStream_to_String(is); 
     } else { 
      return "Hello This is status ==> :" 
        + String.valueOf(statusCode); 
     } 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return null; 

} 

,併爲轉換方法StringBuilder的字符串

public static String iStream_to_String(InputStream is1) { 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096); 
    String line; 
    StringBuilder sb = new StringBuilder(); 
    try { 
     while ((line = rd.readLine()) != null) { 
      sb.append(line); 
     } 
     rd.close(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    String contentOfMyInputStream = sb.toString(); 
    return contentOfMyInputStream; 
}