【问题标题】:Sending HTTP request with JSON body in android app在 Android 应用程序中发送带有 JSON 正文的 HTTP 请求
【发布时间】:2017-05-03 07:30:21
【问题描述】:

好的,伙计们,在过去的 24 年里,我已经尝试了很多关于如何执行此操作的代码示例,但我无法让任何事情起作用。

我只是想将JSON 正文传递给URL。 在BASH 中使用cURL 一切正常,我什至尝试过我们

Runtime.getRuntime().exec("curl.....");

但即使这样也没有通过JSON 正文,实际上我已经成功地通过Runtime.getRuntime().exec(str) 和其他几种方式发送了http 请求,但是其中NONE通过了JSON 正文。

我将不胜感激。

【问题讨论】:

    标签: android androidhttpclient


    【解决方案1】:

    您应该使用改造。 有很多样本here和here's同一个问题

    【讨论】:

      【解决方案2】:

      如果你想编写自己的方法: 这是一个。

      private JsonObject postJsonObject(JsonObject jsonObject) {
              JsonObject nullresult = new JsonObject();
              nullresult.addProperty("success",-1);
              try {
                  String data = jsonObject.toString();
                  URL object = new URL(url); \\MENTION YOUR URL HERE
      
                  HttpURLConnection httpURLConnection = (HttpURLConnection) object.openConnection();
                  httpURLConnection.setDoOutput(true);
                  httpURLConnection.setDoInput(true);
                  httpURLConnection.setRequestProperty("Content-Type", "application/json");
                  httpURLConnection.setRequestProperty("Accept", "application/json");
                  httpURLConnection.setRequestMethod("POST");
      
                  httpURLConnection.setConnectTimeout(8000);
                  httpURLConnection.setReadTimeout(8000);
      
                  OutputStreamWriter wr = new OutputStreamWriter(httpURLConnection.getOutputStream());
                  wr.write(data);
                  wr.flush();
      
                  StringBuilder sb = new StringBuilder();
                  int HttpResult = httpURLConnection.getResponseCode();
                  if (HttpResult == HttpURLConnection.HTTP_OK) {
                      BufferedReader br = new BufferedReader(
                              new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                      String line;
                      while ((line = br.readLine()) != null) {
                          sb.append(line).append("\n");
                      }
                      br.close();
                  }
                  JsonObject result = new JsonParser().parse(sb.toString()).getAsJsonObject();
      
                  if(result !=null)
                      return result;
                  else
                      return nullresult;
              } catch (ProtocolException e) {
                  e.printStackTrace();
              } catch (MalformedJsonException e){
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return nullresult;
          }
      

      您还可以使用Ion 之类的库。 链接:https://github.com/koush/ion

      易于理解的 android 网络库。他们有可以帮助你的例子。

      【讨论】:

        猜你喜欢
        • 2019-03-15
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 2015-03-22
        • 2020-10-04
        • 1970-01-01
        • 2019-08-15
        • 1970-01-01
        相关资源
        最近更新 更多