0

我正在向服务器中的 PHP 站点发送 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
}

}

4

4 回答 4

1

要发出 http 请求,您必须使用 AsyncTask。这将在后台运行。

请参阅下面的链接以供参考

http://www.vogella.com/articles/AndroidPerformance/article.html

于 2012-10-30T05:05:15.663 回答
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 转换为 string 的方法

 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;
}
于 2012-10-30T06:31:57.100 回答
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) {
          }
    }   
于 2012-10-30T05:06:19.763 回答
0

您应该在网络线程之外的其他线程中执行网络操作,请参阅处理程序和异步任务。

http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask

http://developerlife.com/tutorials/?p=290

于 2012-10-30T05:08:54.300 回答