我需要用JSON数据发送POST方法,确保我需要发送JSON对象序列化为字符串。不是JSON字符串本身。我怎样才能实现这个使用JAVA如何使用JSON数据(java)发送POST方法?
1
A
回答
0
1
public static String sendPostRequest(String postURL) throws Exception{
String responseStr=null;
//make POST request
String jsonContent = "{'name': 'newIndia','columns': [{'name': 'Species','type': 'STRING'}],'description': 'Insect Tracking Information.','isExportable': true}";
//String data = "{\"document\" : {\"_id\": \"" + id+ "\", \"context\":" + context +"}}";
URL url = new URL(postURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(jsonContent.getBytes().length));
connection.setUseCaches(false);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(jsonContent);
writer.close();
responseStr="Response code: "+connection.getResponseCode()+" and mesg:"+connection.getResponseMessage();
System.out.println(connection.getResponseMessage());
InputStream response;
// Check for error , if none store response
if(connection.getResponseCode() == 200){
response = connection.getInputStream();
}else{
response = connection.getErrorStream();
}
InputStreamReader isr = new InputStreamReader(response);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(isr);
String read = br.readLine();
while(read != null){
sb.append(read);
read = br.readLine();
}
// Print the String
System.out.println(sb.toString());
connection.disconnect();
return responseStr;
}
欲了解更多,您可以看到这example。
0
使用gson,您可以非常轻松地将JSON数据发布到Web服务。
例如:与
public class Main {
public static void main(String... args) throws Exception {
URL theUrl = new URL("https://robertsspaceindustries.com/api/stats/getCrowdfundStats");
Gson gson = new Gson();
JsonParser jp = new JsonParser();
MyData thedata = new MyData();
HttpsURLConnection urlConnection = (HttpsURLConnection) theUrl.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true); //allow parameters to be sent/appended
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.writeBytes(gson.toJson(thedata)); //convert the POJO to JSON, then to binary.
wr.flush();
wr.close();
urlConnection.connect(); //start request transmission
JsonElement retJson = jp.parse(new InputStreamReader((InputStream) urlConnection.getContent())); //convert the input stream to a json element
System.out.println(retJson.getAsJsonObject());
urlConnection.disconnect(); //end request transmission
}
}
回复:
public class MyData { //var myJsonData = {
private boolean fans = true; // fans:true,
private boolean funds = true; // funds:true
//private String chart = "day"; // }
}
现在发送POJO到真正网络服务
{"success":1,"{"fans":910125,"funds":8410319141},"code":"OK","msg":"OK"}
(注意,等同在写作时借给cURL命令) - >
curl 'https://robertsspaceindustries.com/api/stats/getCrowdfundStats' --data-binary '{"fans":true,"funds":true}'
相关问题
- 1. 如何发送json数据使用C#中的RestSharp POST方法
- 2. 使用$ .Ajax()与POST方法发送JSON
- 3. 如何使用加密/解密使用post(JSON)方法发送加密数据?
- 4. 如何使用getJSON,使用post方法发送数据?
- 5. 发送使用Ajax POST方法与节点的js JSON数据?
- 6. 发送JSON数据使用$ .post
- 7. 用Java发送POST数据
- 8. 如何使用WebView.goBack()方法重新发送POST数据?
- 9. 如何使用POST方法在jQuery中发送数据?
- 10. 如何利用HttpURLConnection类发送POST请求使用JSON数据
- 11. 如何使用LWP发送POST数据?
- 12. 使用Post方法发送XML参数
- 13. 如何使用javax.portlet.ActionResponse.sendRedirect方法发送java中的Post请求?
- 14. 如何使用POST方法将数据发送到URL在Xcode中的方法
- 15. 将json参数发送到api使用post方法Swift 3
- 16. 如何使用post方法将JSON数组发送到服务器?
- 17. 如何使用Java为Blitline发送Json POST请求?
- 18. 如何在PUT方法中发送原始JSON数据在Guzzle POST Body中?
- 19. 发送json对象使用http post方法android应用
- 20. 发送Post方法从Android客户端到Java Servlet的数据
- 21. 使用POST使用HttpURLConnection发送json数据
- 22. HTML to SQL在使用POST方法时发送数据
- 23. 从DART将数据发送到PHP中使用POST方法
- 24. 使用POST方法将数据从Flash发送到PHP
- 25. 使用jQuery通过POST发送和接收数据到java方法
- 26. ClojureScript AJAX POST发送json数据
- 27. 通过POST向PHP发送JSON数据
- 28. 如何通过POST方法发送数据?
- 29. 如何使用POST发送数据并在php中使用json接收
- 30. 如何发送JSON数据
你使用GWT吗? – 2012-04-09 09:03:42
至于POST部分:http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – Thilo 2012-04-09 09:03:50
你是什么意思 - JSON对象序列化为字符串。不是JSON字符串本身? – 2012-04-09 09:05:58