HttpURLConnection,HttpClient和HttpPost请求不能在Android中使用POST请求

时间:2012-05-11 20:18:28

标签: android http-post response httpurlconnection

我尝试了许多不同的方法来获取http响应但总是得到消息null。我尝试过使用chrome附带的Advance Rest Client工具。在那我得到了成功的回应。我不知道我在哪里做错了。

try {

        String urlParameters = "id=userid&password=password&device=android";

        URL url = new URL("my url");
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        urlConnection.setRequestProperty("Content-Length",
                "" + Integer.toString(urlParameters.getBytes().length));
        urlConnection.setRequestProperty("Content-Language", "en-US");

        urlConnection.setUseCaches(false);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(urlParameters.getBytes());
        outputStream.close();

        int responseCode = urlConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream in = urlConnection.getInputStream();
            resultstring = convertinputStreamToString(in);
            Log.d("Result String-------->", resultstring);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

我附上了截图,我得到了结果:成功 但是在上面的代码中我得到了结果:失败

enter image description here

我也尝试过使用HttpClient和HttpPost的代码。但这也是一项工作

    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(
                "my url");

        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("id", "userid"));
        nameValuePairs.add(new BasicNameValuePair("password", "password"));
        nameValuePairs.add(new BasicNameValuePair("device", "android"));

        // httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        UrlEncodedFormEntity se = new UrlEncodedFormEntity(nameValuePairs);
        se.setContentType("application/x-www-form-urlencoded");
        httpPost.setEntity(se);
        httpPost.getParams().setBooleanParameter(
                "http.protocol.expect-continue", false);

        HttpResponse response = client.execute(httpPost);

        String responseAsText = EntityUtils.toString(response.getEntity());
        Log.d("Result String-------->", responseAsText);
    } catch (Exception e) {
        e.printStackTrace();
    }

请帮帮我。我几乎尝试了一切。

1 个答案:

答案 0 :(得分:-1)

HttpClient localHttpClient = this.app2.getHttpClient();
  HttpPost localHttpPost = new HttpPost("yourURL.com");
  localHttpPost.setEntity(new UrlEncodedFormEntity(myArrayList));
  InputStream localInputStream = localHttpClient.execute(localHttpPost).getEntity().getContent();

尝试这样的事情。 您必须将响应作为输入流读取,然后在返回结果后继续工作。

另外,请务必检查服务器,因为它可能没有按预期返回响应。