使用json與外部服務器交互,兩種方式。
第一種,使用阿里json jar,國內項目推薦,優點是不需要依賴包。
第二種,使用jdk原生json jar,需要依賴包(注意哪個版本json要對應哪個版本依賴包),國外項目使用。
依賴包如下:
commons-beanutils-1.8.0.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar
json-lib-2.4-jdk15.jar
httpcore-4.4.10.jar
httpclient-4.5.6.jar
兩種方式只是在jar包使用上不一樣,其他地方一樣。
java代碼如下:
private void doPostJson() {
try {
// サーバ情報解析
// 接続プロトコル
String targetProtocol = PropertiesUtil.getKeyValue(DenbunConst.REAL_FILE_NAME,DenbunConst.CONFIG_PROTOCOL);
// ホスト名
String targetHost = PropertiesUtil.getKeyValue(DenbunConst.REAL_FILE_NAME,DenbunConst.CONFIG_HOST);
// ポート番號
String port = PropertiesUtil.getKeyValue(DenbunConst.REAL_FILE_NAME,DenbunConst.CONFIG_PORT);
// パス
String path = PropertiesUtil.getKeyValue(DenbunConst.REAL_FILE_NAME,DenbunConst.CONFIG_PATH);
StringBuilder sb = new StringBuilder();
sb.append(targetProtocol);
sb.append(DenbunConst.CONST_DELIM_COLON);
sb.append(DenbunConst.CONST_TWOTURA);
sb.append(targetHost);
sb.append(DenbunConst.CONST_DELIM_COLON);
sb.append(port);
sb.append(DenbunConst.CONST_TURA);
sb.append(path);
// String url = "http://192.168.18.183:8080/json";
String url = sb.toString();
// TODO change parameter to variable
JSONObject json = new JSONObject();
json.put("PIM_USER_ID", "ST00000001");
json.put("HASH_VALUE", "043476a25db43d7688dc57fa69bbacdb7b6465208a90c96f841e46a23fd85ae9");
json.put("net_id", "9999");
json.put("mem_id", "userid0000");
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");
httpPost.setEntity(stringEntity);
HttpResponse response = httpclient.execute(httpPost);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if(null!=entity){
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
JSONObject jsonObject = JSONObject.fromObject(result);
// TODO 戻る値の処理を追加
System.out.println(jsonObject);
}else {
// TODO エラー処理を追加
System.out.println("戻る値が空です。");
}
}else {
// TODO エラー処理を追加
throw new ClientProtocolException("Unexpected response status: " + status);
}
} catch (Exception e) {
// TODO catch処理を追加
e.printStackTrace();
}
}
遠程服務器端代碼如下:
import net.sf.json.JSONObject;
@Controller
public class ItemController {
@RequestMapping("/IFP230")
public void if0120(HttpServletRequest request,HttpServletResponse response) {
try {
StringBuilder insb = new StringBuilder();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
insb.append(line);
} catch (Exception e) { /*report an error*/ }
JSONObject jsonObject = JSONObject.fromObject(insb.toString());
System.out.println("injson:"+jsonObject.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
JSONObject respon = new JSONObject();
respon.put("ERROR_INFO", "正常に処理されました。");
String statusVal = "00";
respon.put("status",statusVal);
try {
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(respon.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}