2012-09-12 134 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