【问题标题】:POST Android json dataPOST Android json 数据
【发布时间】:2015-06-08 10:50:42
【问题描述】:

我正在尝试向我的服务器发布一些数据,但我不知道为什么,我总是从中收到 400 条消息。

我想发送这个 POST 结构:

POST /api/v0.1/observations/3 HTTP/1.1
Host: 54.154.117.132`
Content-Type: application/json
Cache-Control: no-cache

{ 
   "data" : 
   { 
      "field1" : 1111111 
   }, 
   "timestamp": "2015-05-13T12:23:42.648738" 
}

如果我使用 curl 或 wget 发送它,我会得到 200 Http 状态。

我在安卓上做这个:

public void POST(String json) throws MalformedURLException {

    int httpResult;
    //URL url = new URL(getString(R.string.URL));
    URL url = new URL(getString(R.string.URL_test2));
    HttpURLConnection con;

    JSONObject obj1 = new JSONObject();
    JSONObject obj2 = new JSONObject();

    try {
        obj1.put("field1",222);
        obj2.put("data", obj1);
        obj2.put("timestamp", "2015-06-8T12:05:42.648738");
    } catch (JSONException e) {
        e.printStackTrace();
    }


    try {
        con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setUseCaches(false);
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        con.setRequestProperty("Content-Type","application/json");
        con.setRequestProperty("Host", "54.154.117.132");
        con.connect();

        OutputStreamWriter out = new   OutputStreamWriter(con.getOutputStream());
        out.write(obj2.toString());
        out.close();

        httpResult = con.getResponseCode();
        Log.i("Request","--> "+httpResult);
        if (httpResult == HttpURLConnection.HTTP_OK) Log.i("HTTP"," --> all ok");
        else Log.i("HTTP", " --> smthg wrong!");

    } catch (IOException e) {
        Log.i("ERROR"," !!! Interrupcion a la hora de conectarse");
    }
}

我不知道这是否是完全相同的结构,这是我的结论,因为在 chrome 上使用 Postman,以及 curl 或 wget,获得 200 状态。

有人可以帮助我吗?

如果你想尝试的 URL 在顶部的 POST 方法上。

【问题讨论】:

  • 如果注释掉“con.connect();”会发生什么
  • 你的 json 语法错误
  • @JunedAhsan 你为什么这么说? Json 是正确的,我在某些 Internet 站点上验证,如果您指的是 Android 代码上的那个,名称键或值不必与顶部的 POST 示例相同。
  • @Knossos 如果评论或删除该行,我会继续收到 400 条消息。

标签: java android json http


【解决方案1】:

在 JSONObject 中添加您的参数,如下所示:

JSONObject jsonObject = new JSONObject();
 jsonObject .put("field", 222);
        JSONObject mainObject = new JSONObject();
        try {
            mainObject .put("data",jsonObject);
            mainObject .put("timestamp","2015-05-13T12:23:42.648738");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

并在此处更改代码:

OutputStreamWriter out = new   OutputStreamWriter(con.getOutputStream());
        out.write(mainObject.toString());  //change to mainObject from obj2
        out.close();

【讨论】:

  • 耶!这样可行!!你能告诉我区别吗?
  • 查看您的 json 字符串 它在其他数据对象中有对象,并且您仅在一个对象中发送所有参数
  • 另一个问题,如果我想发送像“loc”这样的参数:[1.23, 2.76] 我该如何正确地做到这一点??
  • 方括号是 JSONArray,您需要将其作为 JSONArray 发送
  • 这可以吗?: JSONObject mainObject = new JSONObject(); JSONArray loc = new JSONArray(); loc.put((浮点数) 1.233); loc.put((浮点数)2.335); mainObject.put("名字", "荷马 J"); mainObject.put("姓氏", "辛普森"); mainObject.put("loc", loc);
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-26
  • 2020-01-22
  • 2015-02-05
相关资源
最近更新 更多