JAVA接口传递参数(POST),从接口接收数据(JSON) -----记录
1,给接口传递json格式的数据
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONObject;
public class AppAddTest {
public static final String ADD_URL = "http://192.168.1.1:8080/*.controller/*.action";
public static void appadd() {
try {
URL url = new URL(ADD_URL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("connection", "Keep-Alive");
//connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
connection.connect();
//POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
JSONObject obj = new JSONObject();
String message = java.net.URLEncoder.encode("哈哈哈","utf-8");
obj.element("detail", "df");
obj.element("TEXT1", "asd");
obj.element("TEXT2", message);
out.writeBytes("data="+obj.toString());
System.out.println("data="+obj.toString());
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
appadd();
}
}
2.从接口获取json格式数据
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.util.concurrent.ConcurrentHashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.springframework.web.HttpRequestHandler;
public class JAVAURL {
/**
* @param args
*/
public static void main(String[] args) {
String url = "http://192.168.1.1:8080/*.controller.do/*.action";
System.out.println("URL:"+url);
StringBuffer json = new StringBuffer();
try {
//实例一个url和URLConnection
URL oracle = new URL(url);
//打开链接
URLConnection yc = oracle.openConnection();
//输入流作参数传进InputStreamReader并用BufferedReader接受
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine = null;
//一直读到空,并设置流编码是UTF8
while ( (inputLine = in.readLine()) != null) {
json.append(new String(inputLine.getBytes(),"GBK"));
}
//记得关闭连接
in.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONArray jn = JSONArray.fromObject(json.toString());
if(jn.size()>0){
for (int i = 0; i < jn.size(); i++) {
JSONObject jo = (JSONObject) jn.get(i);
System.out.println(jo.get("id"));
System.out.println(jo.get("fdName"));
}
System.out.println(jn);
}
System.out.println("数据大小:"+jn.size());
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接超时!");
}
}
}