使用Java通过HTTP POST发送JSON

时间:2015-06-06 15:42:13

标签: java json

发送方

StringBuilder builder = new StringBuilder();
HttpParams httpParameters=new BasicHttpParams();
HttpResponse response;
HttpPost httpPost = new HttpPost(address);
StringEntity entity = new StringEntity(JSON.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient(httpParameters);
response = client.execute(httpPost);

接收器

StringBuffer jb = new StringBuffer();
String line = null;
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
    jb.append(line);

json_in = org.json.HTTP.toJSONObject(jb.toString());

JSONObject js=new JSONObject((String)json_in.get("Method"));

Map<String, String> dict = new HashMap<>();
Iterator it=js.keys();
while(it.hasNext()){
     String key=(String)it.next();
     dict.put(key,(String) js.get(key));
}

嗨,我正在尝试使用http post传递JSON,错误来自第JSONObject js=new JSONObject((String)json_in.get("Method"));

org.json.JSONException: Unterminated string at 314 [character 315 line 1]

如何解决? 感谢。

编辑:

此请求正常运行

curl http://localhost:8080/ --data "{'a':'b','image':'
/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/4QMraHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjAtYzA2MSA2NC4xNDA5NDksIDIwMTAvMTIvMDctMTA6NTc6MDEgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4w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AAAAAAAAAAAAAAAAAAMBMAAgEDBAIDAQAAAAAAAAAAAAER8CExEGGR0UFRcaGxwf/aAAwDAQACEQMRAAAB5ds7BLDRjahSZDDDBQdmodmJf//aAAgBAQABBQJa5BusxTZ63IlAobhqeTI8jGUD3ucYf//aAAgBAgABBQLSi4IXH//aAAgBAwABBQKi1H//2gAIAQICBj8CH//aAAgBAwIGPwIf/9oACAEBAQY/Ank3HVHMzQuvsPv33GILjATW875plOMyVbaHnXqieeUV6tCdTiacEKse1eHZ46r/2gAIAQEDAT8he9t3d5uN+ujrvxliEj61UxYGkms+2R8plPl+CfRtL+xTRfwdk+eN9B//2gAIAQIDAT8hQbuR+iEkezAQ5yf/2gAIAQMDAT8hbPBIydDPB0f/2gAMAwEAAhEDEQAAECKKez//2gAIAQEDAT8QcqibGJIIOgtEmhHLKXOStTftncTmtpthr9D8NlUyVmt3CFB2lWCkm/ict56Xzov/2gAIAQIDAT8QtIkinCI+g6SRM2tHRCGDODJT0f/aAAgBAwMBPxC4JRKCX0SkbRJ/DN4HjSOB/9k='}"

1 个答案:

答案 0 :(得分:0)

根据Wormbo在评论中说,您的JSON应如下所示:

{"a":"b","image":"/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/4QMraHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjAtYzA2MSA2NC4xNDA5NDksIDIwMTAvMTIvMDctMTA6NTc6MDEgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcm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AgMEIEEUETI0BRURAAECBAQHAAAAAAAAAAAAAAEAAiFRMgMxQRIz8BGBgkODNBIBAAAAAAAAAAAAAAAAAAAAMBMAAgEDBAIDAQAAAAAAAAAAAAER8CExEGGR0UFRcaGxwf/aAAwDAQACEQMRAAAB5ds7BLDRjahSZDDDBQdmodmJf//aAAgBAQABBQJa5BusxTZ63IlAobhqeTI8jGUD3ucYf//aAAgBAgABBQLSi4IXH//aAAgBAwABBQKi1H//2gAIAQICBj8CH//aAAgBAwIGPwIf/9oACAEBAQY/Ank3HVHMzQuvsPv33GILjATW875plOMyVbaHnXqieeUV6tCdTiacEKse1eHZ46r/2gAIAQEDAT8he9t3d5uN+ujrvxliEj61UxYGkms+2R8plPl+CfRtL+xTRfwdk+eN9B//2gAIAQIDAT8hQbuR+iEkezAQ5yf/2gAIAQMDAT8hbPBIydDPB0f/2gAMAwEAAhEDEQAAECKKez//2gAIAQEDAT8QcqibGJIIOgtEmhHLKXOStTftncTmtpthr9D8NlUyVmt3CFB2lWCkm/ict56Xzov/2gAIAQIDAT8QtIkinCI+g6SRM2tHRCGDODJT0f/aAAgBAwMBPxC4JRKCX0SkbRJ/DN4HjSOB/9k="}

我使用漂亮的格式化程序here检查了JSON,它在您的应用中显示了相同的错误。