使用请求正文

时间:2015-12-31 04:38:11

标签: android json parse-platform

我在Android中向parse.com发送帖子请求,并且需要将标题和正文包含在单个json对象中作为帖子请求正文。 这可能是这样的(很可能不是因为我从parse.com响应中收到错误消息:

{
"headers": [
    {
"Content-Type":"application/json",
"X-Parse-Application-Id":"key1",
"X-Parse-REST-API-Key":"key2"

"body": [
    {
        "ephrase": "When the going gets tough, the tough get going.",
        "nvote": 154,
        "pphrase": "Meaning2",
        "yvote": 364
    },
    {
        "ephrase": "Beggars can't be choosers.",
        "nvote": 1,
        "pphrase": "meaning1",
        "yvote": 8
    }
] } 

如果这不起作用,我正在尝试以下操作,但仍然会收到错误“107提供的无效utf-8 tsring”来自parse.com

我将其包含在图片的标题部分:

"Content-Type":"application/json",
"X-Parse-Application-Id":"key1",
"X-Parse-REST-API-Key":"key2"

这是在parse.com期望json对象的主体部分

{ [
    {
        "ephrase": "When the going gets tough, the tough get going.",
        "nvote": 154,
        "pphrase": "",
        "yvote": 364
    },
    {
        "ephrase": "Beggars can't be choosers.",
        "nvote": 1,
        "pphrase": "",
        "yvote": 8
    }
] }

enter image description here

4 个答案:

答案 0 :(得分:1)

在正文中添加标题不是好习惯,标题需要单独发送。

如果您使用HTTPPost发送请求,则可以使用addheader函数

添加标头
httppost.addHeader("YOUR-KEY", "YOUR-value");

在你的情况下,这将是

 httppost.addHeader("Content-Type", "application/json");  
 httppost.addHeader("X-Parse-Application-Id", "key1");  
 httppost.addHeader("X-Parse-REST-API-Key", "key2");  

希望这会对你有所帮助

答案 1 :(得分:0)

标头不是JSON主体的一部分。标题是请求的单独部分。只有身体内容应该是您发送的json的一部分。

答案 2 :(得分:0)

我建议您使用HTTPURLConnection而不是其他已弃用的apis。

    String urlStr = "";
    HttpURLConnection conn;
    URL url1;
    String jsonBody = "{ [\n" +
            "    {\n" +
            "        \"ephrase\": \"When the going gets tough, the tough get going.\",\n" +
            "        \"nvote\": 154,\n" +
            "        \"pphrase\": \"\",\n" +
            "        \"yvote\": 364\n" +
            "    },\n" +
            "    {\n" +
            "        \"ephrase\": \"Beggars can't be choosers.\",\n" +
            "        \"nvote\": 1,\n" +
            "        \"pphrase\": \"\",\n" +
            "        \"yvote\": 8\n" +
            "    }\n" +
            "] }";

    try {

        JSONObject jObj = new JSONObject(jsonBody);

        url1 = new URL(urlStr);
        conn = (HttpURLConnection) url1.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("X-Parse-Application-Id", "key1");
        conn.setRequestProperty("X-Parse-REST-API-Key", "key2");
        conn.setDoOutput(true);
        conn.connect();

        OutputStream os = conn.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        osw.write(jObj.toString());
        osw.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

请确保你在AsyncTask的doInBackground()中执行上述操作,否则线程就可以了。

答案 3 :(得分:0)

我发现此示例包含所有类型的HTTP:

https://github.com/loopj/android-async-http

如果您使用此项,则需要将以下代码添加到JsonStreamerSample.java,以允许您包含标题。

public boolean isRequestHeadersAllowed() {
    return true;
}

我仍有一个小问题,但在下面的问题中会进一步解释。

Passing JSON object to parse.com in Android