Android Http Get Post

 public class MyHttpUrlCon {

     public static String settionId = "";

     public static final int TIMEOUT = ;//

     public ReturnData doGet(String urlString) {
HttpURLConnection conn = null;
try { URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout();
conn.setReadTimeout();
conn.setRequestMethod("GET");
conn.setRequestProperty("cookie", settionId);
conn.connect(); if (conn.getHeaderField("set-cookie") != null) {
settionId = conn.getHeaderField("set-cookie");
Log.d("mm", "Constants.settionId:" + settionId);
} else {
return new ReturnData(ReturnData.TimeOut, "");
} if (conn.getResponseCode() == ) {
InputStream is = conn.getInputStream();
BufferedReader bf = new BufferedReader(
new InputStreamReader(is));
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = bf.readLine()) != null) {
sb.append(line);
}
String jsonString = sb.toString().replace("\n", "\\n")
.replace("\r", "\\r");
String jsonStr = jsonString.substring(jsonString.indexOf("{"),
jsonString.length()); return new ReturnData(ReturnData.Succee, jsonStr); } else {
return new ReturnData(ReturnData.NoRespond, "");
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new ReturnData(ReturnData.NoRespond, "");
} finally {
if (conn != null) {
conn.disconnect();
}
}
} public ReturnData doPost(String urlString, String params) {
URL url;
HttpURLConnection conn = null;
InputStream input = null;
try {
url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(); // 请求超时
conn.setRequestMethod("POST"); // 请求方式
conn.setReadTimeout(); // 读取超时
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// conn.setRequestProperty("cookie", settionId);
OutputStream os = conn.getOutputStream();
System.out.println("请求的参数" + params);
os.write(params.getBytes("utf-8"));
os.close(); if (conn != null) {
int code = conn.getResponseCode();
System.out.println(code + ":");
if (code == ) {
input = conn.getInputStream();
BufferedReader bf = new BufferedReader(
new InputStreamReader(input));
String line = "";
StringBuilder sbuilder = new StringBuilder();
while ((line = bf.readLine()) != null) {
sbuilder.append(line);
}
System.out.println(sbuilder.toString());
String jsonString = sbuilder.toString()
.replace("\n", "\\n").replace("\r", "\\r");
String jsonStr = jsonString.substring(
jsonString.indexOf("{"), jsonString.length());
return new ReturnData(ReturnData.Succee, jsonStr);
} else {
return new ReturnData(ReturnData.NoRespond, "");
}
} else {
return new ReturnData(ReturnData.NoRespond, "");
}
} catch (Exception e) {
e.printStackTrace();
return new ReturnData(ReturnData.NoRespond, "");
} finally {
if (conn != null) {
conn.disconnect();
}
}
} }

public class ReturnData {

public final static int ParserError = -3;
public final static int ParamsError = -2;
public final static int False = -1;
public final static int Succee = 0;
public final static int TimeOut = 1;
public final static int NoRespond = 2;

// 网络请求标记
private int code;
// 返回数据
private String data;

public ReturnData(int code, String data) {
this.code = code;
this.data = data;
}

/**
* 网络请求标记
*
* @return
*/
public int getCode() {
return code;
}

/**
* 网络请求标记
*
* @param code
*/
public void setCode(int code) {
this.code = code;
}

/**
* 返回数据
*
* @return
*/
public String getData() {
return data;
}

/**
* 返回数据
*
* @param data
*/
public void setData(String data) {
this.data = data;
}

}

上一篇:『TensorFlow』滑动平均


下一篇:Spring实战第九章学习笔记————保护Web应用