2015-12-31 22 views
0

我在Android中向parse.com發送了一個發佈請求,並且需要在單個json對象中包含標題和正文作爲發佈請求主體。 這可能是這樣的(很可能不是因爲我遇到錯誤信息從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

+0

從哪裏發送此請求?你用什麼客戶來打這個電話? – shiladitya

+0

您上面發佈了錯誤的json結構。請確定它是提取 – GiapLee

+0

爲什麼你需要發送標題中的密鑰?這些應該是你的應用程序的祕密。 –

回答

0

頭是不是JSON身體的一部分。標頭是請求的獨立部分。只有正文內容應該是您發送的json的一部分。

+0

我有一個小的代碼,這也允許我包括單獨的headrers和bod,你們會知道這可能是如何工作的。 – Amir

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"); 

希望這將幫助你

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裏面的東西()或線程只會被罰款。

+0

如果您不打算認真解決問題,請刪除此答案。 – Amir

+0

@阿米爾:我可能誤解了你的問題,你可能會誤告我嗎? – kevz

+0

@Amir:我已經編輯我的答案傳遞的JSONObject。 – kevz