【问题标题】:Post Parameters in Android RequestAndroid 请求中的 Post 参数
【发布时间】:2013-01-05 12:20:42
【问题描述】:

我正在做一个 Android 应用程序,但我在对自己的服务器执行请求时遇到问题。我已经使用 Play Framework 制作了服务器,并从 Json 中获取了参数:

 response.setContentTypeIfNotSet("application/json; charset=utf-8");        
 JsonParser jsonParser = new JsonParser(); 
 JsonElement jsonElement = jsonParser.parse(getBody(request.body)); 
 Long id =jsonElement.getAsJsonObject().get("id").getAsLong();

当我向我的服务器发出 GET 请求时,一切正常。但是当我发出 POST 请求时,我的服务器返回一个未知错误,可能是 JSON 格式错误或找不到元素。

私有 ArrayList NameValuePair> 参数;

私有 ArrayList NameValuePair> 标头;

...

案例发布:

  HttpPost postRequest = new HttpPost(host);
  // Add headers
  for(NameValuePair h : headers) 
  {
       postRequest.addHeader(h.getName(), h.getValue());
  }
  if(!params.isEmpty())
  {
       postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  }
  executeRequest(postRequest, host);
  break;

我尝试过处理请求的参数,但失败了:

if(!params.isEmpty()) {
HttpParams HttpParams = new BasicHttpParams();

 for (NameValuePair param : params)
 {
      HttpParams.setParameter(param.getName(), param.getValue());
 }                
 postRequest.setParams(HttpParams); }

还有不同的错误,取决于我提出的要求。它们都是'play.exceptions.JavaExecutionException':

  • 'com.google.gson.stream.MalformedJsonException'
  • '这不是 JSON 对象'
  • '期望找到对象:“id”'

我希望有人可以帮助我。

【问题讨论】:

    标签: android post parameters request


    【解决方案1】:

    这是发送 HTTP Post 的简单方法。

    HttpPost httppost = new HttpPost("Your URL here");
    httppost.setEntity(new StringEntity(paramsJson));
    httppost.addHeader("content-type", "application/json");
    HttpResponse response = httpclient.execute(httppost);
    

    你最好直接使用 JSON 字符串而不是在这里解析它。希望对你有帮助

    【讨论】:

    • 但我有一个 List 参数用于添加到请求中。如果我以字符串格式制作自己的 JSON,我必须手动完成吗?谢谢大家。
    • 您好 Neo,我已根据您的建议发布了 JSON。如何查询ASP.Net服务器中的参数?
    【解决方案2】:
     Try this,It may help u
    
         public void executeHttpPost(String string) throws Exception
        {
            //This method for HttpConnection
               try
              {
                 HttpClient client = new DefaultHttpClient();
    
                 HttpPost request = new HttpPost("URL");
    
               List<NameValuePair> value=new ArrayList<NameValuePair>();
    
               value.add(new BasicNameValuePair("Name",string));
    
               UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);
    
               request.setEntity(entity);
    
              client.execute(request);
    
               System.out.println("after sending :"+request.toString());
    
               }
            catch(Exception e)  {System.out.println("Exp="+e);
              }
    
     }
    

    【讨论】:

    • 这正是我所做的,因为在我的代码参数中是一个 List。显然这对我不起作用:S 非常感谢任何地方=)
    • E/错误用户(371):错误 500::'play.exceptions.JavaExecutionException',01-22 10:43:06.582:E/错误用户(371):消息:com.google .gson.stream.MalformedJsonException: 使用 jsonReader.setLenient(true) 在 idUser1=57&idUser2=65 附近接受格式错误的 JSON E/Error Users(371): ' E/Error Users(371): Error 500: : 'play.exceptions. JavaExecutionException',E/错误用户(371):消息:'这不是 JSON 对象。' E/Error Users(371): Error 500: : 'play.exceptions.JavaExecutionException', E/Error Users(371): message: 'Expecting object found: "name"'
    • 对不起,糟糕的演讲。
    • 什么 JSON?我有一个 NameValuePair 的 ArrayList,我唯一能做的就是 postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));我不在 Android 应用程序中做任何 JSON。 (错误来自服务器)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2021-03-11
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多