Android Http Get Post

  1 public class MyHttpUrlCon {
  2 
  3     public static String settionId = "";
  4 
  5     public static final int TIMEOUT = 30;//
  6 
  7     public ReturnData doGet(String urlString) {
  8         HttpURLConnection conn = null;
  9         try {
 10 
 11             URL url = new URL(urlString);
 12             conn = (HttpURLConnection) url.openConnection();
 13             conn.setConnectTimeout(10000);
 14             conn.setReadTimeout(10000);
 15             conn.setRequestMethod("GET");
 16             conn.setRequestProperty("cookie", settionId);
 17             conn.connect();
 18 
 19             if (conn.getHeaderField("set-cookie") != null) {
 20                 settionId = conn.getHeaderField("set-cookie");
 21                 Log.d("mm", "Constants.settionId:" + settionId);
 22             } else {
 23                 return new ReturnData(ReturnData.TimeOut, "");
 24             }
 25 
 26             if (conn.getResponseCode() == 200) {
 27                 InputStream is = conn.getInputStream();
 28                 BufferedReader bf = new BufferedReader(
 29                         new InputStreamReader(is));
 30                 String line = "";
 31                 StringBuilder sb = new StringBuilder();
 32                 while ((line = bf.readLine()) != null) {
 33                     sb.append(line);
 34                 }
 35                 String jsonString = sb.toString().replace("\n", "\\n")
 36                         .replace("\r", "\\r");
 37                 String jsonStr = jsonString.substring(jsonString.indexOf("{"),
 38                         jsonString.length());
 39 
 40                 return new ReturnData(ReturnData.Succee, jsonStr);
 41 
 42             } else {
 43                 return new ReturnData(ReturnData.NoRespond, "");
 44             }
 45 
 46         } catch (IOException e) {
 47             // TODO Auto-generated catch block
 48             e.printStackTrace();
 49             return new ReturnData(ReturnData.NoRespond, "");
 50         } finally {
 51             if (conn != null) {
 52                 conn.disconnect();
 53             }
 54         }
 55     }
 56 
 57     public ReturnData doPost(String urlString, String params) {
 58         URL url;
 59         HttpURLConnection conn = null;
 60         InputStream input = null;
 61         try {
 62             url = new URL(urlString);
 63             conn = (HttpURLConnection) url.openConnection();
 64             conn.setConnectTimeout(10000); // 请求超时
 65             conn.setRequestMethod("POST"); // 请求方式
 66             conn.setReadTimeout(1000); // 读取超时
 67             conn.setDoOutput(true);
 68             conn.setDoInput(true);
 69             conn.setUseCaches(false);
 70             conn.setRequestProperty("Charset", "UTF-8");
 71             conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
 73             // conn.setRequestProperty("cookie", settionId);
 74             OutputStream os = conn.getOutputStream();
 75             System.out.println("请求的参数" + params);
 76             os.write(params.getBytes("utf-8"));
 77             os.close();
 78 
 79             if (conn != null) {
 80                 int code = conn.getResponseCode();
 81                 System.out.println(code + ":");
 82                 if (code == 200) {
 83                     input = conn.getInputStream();
 84                     BufferedReader bf = new BufferedReader(
 85                             new InputStreamReader(input));
 86                     String line = "";
 87                     StringBuilder sbuilder = new StringBuilder();
 88                     while ((line = bf.readLine()) != null) {
 89                         sbuilder.append(line);
 90                     }
 91                     System.out.println(sbuilder.toString());
 92                     String jsonString = sbuilder.toString()
 93                             .replace("\n", "\\n").replace("\r", "\\r");
 94                     String jsonStr = jsonString.substring(
 95                             jsonString.indexOf("{"), jsonString.length());
 96                     return new ReturnData(ReturnData.Succee, jsonStr);
 97                 } else {
 98                     return new ReturnData(ReturnData.NoRespond, "");
 99                 }
100             } else {
101                 return new ReturnData(ReturnData.NoRespond, "");
102             }
103         } catch (Exception e) {
104             e.printStackTrace();
105             return new ReturnData(ReturnData.NoRespond, "");
106         } finally {
107             if (conn != null) {
108                 conn.disconnect();
109             }
110         }
111     }
112 
113 }


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;
}

}

 

你可能感兴趣的:(android)