Examples are as follows:
import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import org.json.JSONArray;import org.json.JSONObject;public class GetJsonData {public static String getJsonData(JSONObject jsonParam,String urls) {StringBuffer sb=new StringBuffer();try {;// Create url resource URL url = new URL(urls);// Create http connection HttpURLConnection conn = (HttpURLConnection) url.openConnection();// Set allow output conn.setDoOutput(true); // Set allow input conn.setDoInput(true); // Set no cache conn.setUseCaches(false); // Set transfer method conn.setRequestMethod("POST"); // Set maintain long connection conn.setRequestProperty("Connection", "Keep-Alive"); // Set file character set: conn.setRequestProperty("Charset", "UTF-8"); // Convert to byte array byte[] data = (jsonParam.toString()).getBytes(); // Set file length conn.setRequestProperty("Content-Length", String.valueOf(data.length)); // Set file type: conn.setRequestProperty("contentType", "application/json"); // start connection request connect.connect(); OutputStream out = new DataOutputStream(conn.getOutputStream()) ;// Write the request string out.write((jsonParam.toString()).getBytes());out.flush();out.close();System.out.println(conn.getResponseCode());// The status returned by the request is if (HttpURLConnection.HTTP_OK == conn.getResponseCode(){System.out.println("Connection successful");// The data returned by the request InputStream in1 = conn.getInputStream();try { String readLine=new String(); BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8")); while((readLine=responseReader.readLine())!=null){ sb.append(readLine).append("/n"); } responseReader.close(); System.out.println(sb.toString());} catch (Exception e1) {e1.printStackTrace();}} else {System.out.println("error++");}} catch (Exception e) {}return sb.toString();}public static void main(String[] args) {JSONObject jsonParam = new JSONObject();jsonParam.put("id", "1401_1406");jsonParam.put("device_size", "480x720");String url="www.baidu.com";String data=GetJsonData.getJsonData(jsonParam,url); //Return a string in [{}] format: JSONArray jsonArray = new JSONArray(data); //Return a string in {} format: /*JSONObject obj= new JSONObject(data);*/ }}The above java method to return json data by sending json and post requests is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.