public String httpPost(String urlStr, ArrayList<HashMap<String, Object>> list){
URL connect;
StringBuffer responseData = new StringBuffer();
try {
connect = new URL(urlStr);
//開啓連接
HttpURLConnection connection = (HttpURLConnection) connect.openConnection();
/*爲連接設置參數*/
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setDefaultUseCaches(false); //post方法禁用緩存
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
JSONArray jsonArray = JSONArray.fromObject(list);
String str = "str="+jsonArray.toString();
writer.write(str);
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line=reader.readLine()) != null) {
responseData.append(line);
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
return responseData.toString();
}
調用遠程接口,返回數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.