JAvaバックグラウンドgetとpostリクエストアクセスインタフェース
4317 ワード
get他のプロジェクトインタフェースへのアクセス
Postアクセスコード
/**
* @param deviceNo
* @param data
* @return
*/
@ResponseBody
@RequestMapping(value = "status")
public JSONObject status(String deviceNo,String data) {
JSONObject jsonObject = new JSONObject ();
Integer status = 1 ;
try {
String date ="";
if((deviceNo!=null&&deviceNo!="")&&(data!=null&&data!="")){
//
date = "deviceNo="+deviceNo+"&runState="+data;
String path="http://api.kdcm.net.cn/api/terminals/run-state/update?"+date;
// System.out.println(path);
URL url = new URL(path); // URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//
connection.setRequestProperty("accept", "application/json");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
connection.connect();//
//
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {//
sb.append(line);
}
br.close();//
connection.disconnect();//
}else {
status=2;
}
} catch (Exception e) {
e.printStackTrace();
}
jsonObject.put("status", status);
return jsonObject;
}
Postアクセスコード
public void push(String space,String controll,String deviceNo,String status ,String version ,String url,String attach){
String path="http://api.kdcm.net.cn/api/terminals/remote-control/data";
HashMap paramMap = new HashMap();
if(Integer.parseInt(controll)==3){ //
paramMap.put("space", space);
}else if (Integer.parseInt(controll)==6) {//
paramMap.put("version", version);
}else {
paramMap.put("url", url); //
paramMap.put("attach", attach); //
}
paramMap.put("type", controll);
paramMap.put("deviceNo", deviceNo);
paramMap.put("status", status);
// , map
NetTool.sendPost(path, paramMap);
}
/**
* post
* json
* @param baseUrl
* @param paramMap
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String sendPost(String baseUrl, HashMap paramMap) {
String strResult = "";
try {
HttpPost request = new HttpPost(baseUrl);
HttpClient httpclient = new DefaultHttpClient();
if (paramMap != null && paramMap.size() > 0) {
List paramList = getParams(paramMap);
request.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
}
// 5
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
// 5
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
HttpResponse httpResponse = httpclient.execute(request);
httpResponse.setHeader("Authorization", "APPCODE 0930954d870348e688762dc9fdb20f37");
Integer status = httpResponse.getStatusLine().getStatusCode();
//
System.out.println(status);
if (status == HttpStatus.SC_OK) {
//
httpResponse.getEntity().getContentLength();
strResult = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
if (strResult.startsWith("\"")) {
strResult = strResult.substring(1, strResult.length() - 1);
}
} else {
strResult = "faile";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
strResult = "faile";
} catch (IOException e) {
e.printStackTrace();
strResult = "faile";
}
// System.out.println(strResult);
return strResult;
}