android 同步http,android http同步请求

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值