Android HTTPPost和HTTPResponse错误

时间:2013-10-25 14:53:29

标签: php android http-post

我正在开发一款应用并遇到此问题。我使用PHP作为后端服务器,使用JSON作为数据传输技术。但问题是,Http POST和RESPONSE无效。 Http GET正在运行,用户正在登录,但没有回复,POST也无法正常工作。

如果您了解问题,请帮助我。

    // Making HTTP request
    try {
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeOut);                                   HttpConnectionParams.setSoTimeout(httpParameters, timeOut);                                                                       
        HttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpEntity httpEntity = null;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        httpEntity = httpResponse.getEntity();

}

1 个答案:

答案 0 :(得分:0)

尝试这样:

HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;

    try { 
       URI url = new URI("xxxxxxxxxxxxxxxxxxxxxx");
       HttpPost post = new HttpPost(url);
       JSONObject json = new JSONObject();
       json.put("x",x);
       json.put("y", y);        

       StringEntity se = new StringEntity(holder.toString());
       post.setEntity(se);
       response = client.execute(post);

       if(response!=null){
           InputStream in = response.getEntity().getContent(); //Get the data in the entity
           in.close();
        }
    } catch(Exception e) {
      e.printStackTrace();
    }