【问题标题】:Can't get an http JSON response in the post http request无法在 post http 请求中获得 http JSON 响应
【发布时间】:2012-09-12 14:57:08
【问题描述】:

我正在尝试在以下代码中接收 http 响应:

public void httpRes (){
try {
    HttpClient client = new DefaultHttpClient();
    String postURL = "http://validate.jsontest.com/";
    HttpPost post = new HttpPost(postURL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user", "kris"));
        params.add(new BasicNameValuePair("pass", "xyz"));
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);
        HttpEntity resEntity = responsePOST.getEntity();
        if (resEntity != null) {
            Log.i("RESPONSE",EntityUtils.toString(resEntity));
        }
} catch (Exception e) {
    e.printStackTrace();
}}

出现错误 -

LogCat 日志:

09-12 21:48:58.099: INFO/RESPONSE(28485): {
        "error": "No JSON to validate. Please post JSON to validate via the json parameter.",
        "validate": false
        }

我尝试接收来自 GET 请求的响应 - 一切正常,但是当我尝试 POST 请求各种 POST 代码时 - 我无法得到答案!问题是什么?需要帮助。

我也试过了:

Map<String, String> comment = new HashMap<String, String>();
comment.put("password", "password");
comment.put("avatar", "httpssssssss");

String json = new GsonBuilder().create().toJson(comment, Map.class);
HttpResponse response = makeRequest("http://validate.jsontest.com/", json);
//Log.w(TAG, EntityUtils.toString(response));
try {
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    String sss= convertStreamToString(is);
    Log.w("SSSSSSSSSSSSSSSSSS", sss);
} catch (Exception ex) {
    Log.w("Exception exxx", ex);
}
public static HttpResponse makeRequest(String uri, String json) {
        try {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(new StringEntity(json));
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            return new DefaultHttpClient().execute(httpPost);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append((line + "\n"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

那么,问题出在哪里?

【问题讨论】:

  • 我在这里有点困惑,你没有向那个 URl 发布任何 json,你只是在编码 POST 参数,所以它当然会告诉你没有要验证的 json。
  • 代码的第一部分好的,我理解我的错误,但是第二部分呢?

标签: android json post request response


【解决方案1】:

http://validate.jsontest.com 拒绝 POST 请求,除非 Content-Type 标头设置为 application/x-www-form-urlencoded

【讨论】:

    【解决方案2】:

    通过获取您的代码行

    HttpPost httpPost = new HttpPost(uri);
    

    应该是

    HttpPost httpPost = new HttpPost(uri+"?json="+json);
    

    我认为你不需要下面的三行。

    通过 POST 你需要像这样传递 json 参数。

    params.add(new BasicNameValuePair("json", json));
    

    假设 json 是一个包含您的 json 数据的变量。

    您可以在浏览器中测试 GET 方法,例如 http://validate.jsontest.com/?json={validate: true}

    【讨论】:

      【解决方案3】:

      您需要将有效的 json 发布到 url http://validate.jsontest.com/ 以验证它。你所做的基本上是向 url 发送一个 post 请求,它不是 json 格式的。 您必须将其编码为 json,然后将其发送到验证 url。

      【讨论】: