public String testhttps(String myurl,String str) {
responseresult = postjson(myurl, str, AppContext.getContext());
Log.e("aa", "第一次服务器返回数据 " + responseresult);
return responseresult;
// JSONObject jsonObject= JSONObject.parseObject(responseresult);
// int aa=(Integer) jsonObject.get("result");
// Log.e("aa", "testhttps: "+ aa);
}
public String testhttps2(String myurl,String str) {
try {
responseresult = readContentFromPost(myurl, str);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("aa", "服务器返回数据 " + responseresult);
return responseresult;
}
private String readContentFromPost(final String getUrl, final String datajson) throws IOException {
// Post请求的url,与get不同的是不需要带参数
URL postUrl = new URL(getUrl);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
// http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// 默认是 GET方式
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
// 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
String content = "Command=" + URLEncoder.encode(datajson, "UTF-8");
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(content);
//流用完记得关
out.flush();
out.close();
//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder stringBuilder=new StringBuilder();
while ((line = reader.readLine()) != null){
System.out.println(line);
stringBuilder.append(line);
}
reader.close();
//该干的都干完了,记得把连接断了
connection.disconnect();
return stringBuilder.toString();
}
private String postjson(final String getUrl, final String datajson, final Context context) {
try {
System.out.println("Send to:"+getUrl);
System.out.println("Send data:"+datajson);
URL connectUrl = new URL(getUrl);
HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setConnectTimeout(5000);
conn.setReadTimeout(45000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
BufferedWriter oWriter = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
oWriter.write(datajson);
oWriter.flush();
InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(isr);
int code = conn.getResponseCode();
String myresult = "无结果返回";
if (200 == code) {
String temp = reader.readLine();
reader.close();
if (temp != null) {
return temp;
}
}
return myresult;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}