1 packagecom.example.getdata.service;2
3 importjava.io.IOException;4 importjava.io.InputStream;5 importjava.io.OutputStream;6 importjava.io.UnsupportedEncodingException;7 importjava.net.HttpURLConnection;8 importjava.net.URL;9 importjava.net.URLEncoder;10 importjava.util.ArrayList;11 importjava.util.List;12
13 importorg.apache.http.HttpResponse;14 importorg.apache.http.NameValuePair;15 importorg.apache.http.client.ClientProtocolException;16 importorg.apache.http.client.HttpClient;17 importorg.apache.http.client.entity.UrlEncodedFormEntity;18 importorg.apache.http.client.methods.HttpGet;19 importorg.apache.http.client.methods.HttpPost;20 importorg.apache.http.impl.client.DefaultHttpClient;21 importorg.apache.http.message.BasicNameValuePair;22
23 importcom.example.getdata.utils.StreamTools;24 /*
25 * 注意事项:在发送请求时,如果有中文,注意把它转换为相应的编码26 */
27 public classLoginService {28
29 public staticString loginByGet(String username, String password){30 try{31 String path = "http://192.168.1.100/android/index.php?username=" + URLEncoder.encode(username, "utf-8") + "&password=" +password;32 URL url = newURL(path);33 HttpURLConnection conn =(HttpURLConnection)url.openConnection();34 conn.setRequestMethod("GET");35 conn.setReadTimeout(5000);36 //conn.setRequestProperty(field, newValue);
37 int code =conn.getResponseCode();38 if(code == 200){39 InputStream is =conn.getInputStream();40 String result =StreamTools.readInputStream(is);41 returnresult;42 }else{43 return null;44 }45 } catch(Exception e) {46 //TODO Auto-generated catch block
47 e.printStackTrace();48 return null;49 }50
51 }52
53 public staticString loginByPost(String username, String password){54 String path = "http://192.168.1.101/android/index.php";55 try{56 URL url = newURL(path);57 HttpURLConnection conn =(HttpURLConnection)url.openConnection();58 conn.setRequestMethod("POST");59 conn.setReadTimeout(5000);60 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");61 String data = "username=" + URLEncoder.encode(username, "utf-8") + "&password=" +password;62 conn.setRequestProperty("Content-Length", data.length() + "");63 //允许向外面写数据
64 conn.setDoOutput(true);65 //获取输出流
66 OutputStream os =conn.getOutputStream();67 os.write(data.getBytes());68 int code =conn.getResponseCode();69 if(code == 200){70 InputStream is =conn.getInputStream();71 String result =StreamTools.readInputStream(is);72 returnresult;73 }else{74 System.out.println("----111");75 return null;76 }77 } catch(Exception e) {78 //TODO Auto-generated catch block
79 e.printStackTrace();80 System.out.println("----2222");81 return null;82 }83
84 }85
86 public staticString loginByClientGet(String username, String password){87 try{88 //1、打开一个浏览器
89 HttpClient client = newDefaultHttpClient();90
91 //输入地址?
92 String path = "http://192.168.1.101/android/index.php?username=" + URLEncoder.encode(username, "utf-8") + "&password=" +password;93 HttpGet httpGet = newHttpGet(path);94
95 //敲回车
96 HttpResponse response =client.execute(httpGet);97
98 //获取返回状态码
99 int code =response.getStatusLine().getStatusCode();100 if(code == 200){101 InputStream is =response.getEntity().getContent();102 String result =StreamTools.readInputStream(is);103 returnresult;104 }else{105 System.out.println("----111");106 return null;107 }108 }catch(Exception e){109 e.printStackTrace();110 System.out.println("----222");111 return null;112 }113
114 }115
116 public staticString loginByClientPost(String username, String password){117 try{118 //???????
119 HttpClient client = newDefaultHttpClient();120
121 //??????
122 String path = "http://192.168.1.101/android/index.php";123 HttpPost httpPost = newHttpPost(path);124 //??????????????
125 List parameters = new ArrayList();126 parameters.add(new BasicNameValuePair("username",username));127 parameters.add(new BasicNameValuePair("password",password));128
129 httpPost.setEntity(new UrlEncodedFormEntity(parameters,"utf-8"));130 HttpResponse response =client.execute(httpPost);131 //??????????
132 int code =response.getStatusLine().getStatusCode();133 if(code == 200){134 InputStream is =response.getEntity().getContent();135 String result =StreamTools.readInputStream(is);136 returnresult;137 }else{138 System.out.println("----111");139 return null;140 }141 } catch(Exception e) {142 //TODO Auto-generated catch block
143 System.out.println("----222");144 e.printStackTrace();145 return null;146 }147 }148 }