2012-09-12 133 views
1

我嘗試收到如下因素代碼HTTP響應:無法獲取的POST HTTP請求的HTTP JSON響應

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(); 
    } 

那麼,是什麼問題?

+0

我在這裏有點困惑,你沒有發佈任何json到那個URl,你只是編碼POST參數,所以當然它會告訴你,沒有json來驗證。 – brthornbury

+0

代碼的第一部分好的,我明白我的錯誤,但第二部分呢? – user1666141

回答

-1

你需要發佈一個有效的json到url http://validate.jsontest.com/來驗證它。你正在做的是基本上向url發送一個post請求,該請求不是json格式。 您必須將其編碼爲json,然後將其發送到驗證網址。

+0

閱讀評論.. – brthornbury

0

通過GET你的代碼

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= {驗證:真}除非Content-Type首部設置爲application/x-www-form-urlencoded