簡體   English   中英

在發布http請求中無法獲得http JSON響應

[英]Can't get an http JSON response in the post http request

我嘗試通過以下代碼接收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 requsets接收響應-一切正常,但是當我嘗試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();
    }

那么,有什么問題呢?

除非Content-Type標頭設置為application/x-www-form-urlencoded否則http://validate.jsontest.com拒絕POST請求。

通過獲取您的代碼行

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}

您需要將有效的json發布到url http://validate.jsontest.com/進行驗證。 您要做的基本上是向url發送發布請求,該請求不是json格式。 您必須將其編碼為json,然后將其發送到驗證網址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM