Android 通过http协议数据交互
Android 通过http协议数据交互
方式一:HttpPost(import org.apache.http.client.methods.HttpPost)
java代码:
01.
02.private Button button1, button2, button3;
03.private TextView textView1;
04.
05.
06.button1.setOnClickListener(new Button.OnClickListener() {
07.
08.
10.
11.public void onClick(View arg0) {
12.// TODO Auto-generated method stub
13.// URLַ
14.// String uriAPI =
16.String uriAPI = ;论文网
17.
18.
19./* 建立HTTP Post连线 */
20.HttpPost httpRequest = new HttpPost(uriAPI);
21.// Post运作传送变数必须用NameValuePair[]阵列储存
22.// 传参数 服务端获取的方法为request.getParameter("name")
23.List<NameValuePair> params = new ArrayList<NameValuePair>();
24.params.add(new BasicNameValuePair("name", "this is post"));
25.try {
26.
27.
28.// 发出HTTP request
29.httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
30.// 取得HTTP response
31.HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
32.
33.
34.// 若状态码为200 ok
35.if (httpResponse.getStatusLine().getStatusCode() == 200) {
36.// 取出回应字串
37.String strResult = EntityUtils.toString(httpResponse.getEntity());
38.textView1.setText(strResult);
39.} else {
40.textView1.setText("Error Response" + httpResponse.getStatusLine().toString());
41.}
42.} catch (ClientProtocolException e) {
43.textView1.setText(e.getMessage().toString());
44.e.printStackTrace();
45.} catch (UnsupportedEncodingException e) {
46.textView1.setText(e.getMessage().toString());
47.e.printStackTrace();
48.} catch (IOException e) {
49.textView1.setText(e.getMessage().toString());
50.e.printStackTrace();
51.}
52.}
53.
54.});
55.
复制代码
方式二:HttpURLConnection、URL(import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;)
java代码:
01.
02.private void httpUrlConnection() {
03.try {
04.String pathUrl = "http://172.20.0.206:8082/TestServelt/login.do";
05.// 建立连接
06.URL url = new URL(pathUrl);
07.HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
08.
09.
10.// //设置连接属性
11.httpConn.setDoOutput(true);// 使用 URL 连接进行输出
12.httpConn.setDoInput(true);// 使用 URL 连接进行输入
13.httpConn.setUseCaches(false);// 忽略缓存
14.httpConn.setRequestMethod("POST");// 设置URL请求方法
15.String requestString = "客服端要以以流方式发送到服务端的数据...";
16.
17.
18.// 设置请求属性
19.// 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
20.byte[] requestStringBytes = requestString.getBytes(ENCODING_UTF_8);
21.httpConn.setRequestProperty("Content-length", "" + requestStringBytes.length);
22.httpConn.setRequestProperty("Content-Type", "application/octet-stream");
23.httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
24.httpConn.setRequestProperty("Charset", "UTF-8");
25.//
26.String name = URLEncoder.encode("黄武艺", "utf-8");
27.httpConn.setRequestProperty("NAME", name);
28.
29.
30.// 建立输出流,并写入数据
31.OutputStream outputStream = httpConn.getOutputStream();
32.outputStream.write(requestStringBytes);
33.outputStream.close();
34.// 获得响应状态
35.int responseCode = httpConn.getResponseCode();
36.
37.
38.if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
39.// 当正确响应时处理数据
40.StringBuffer sb = new StringBuffer();
41.String readLine;
42.BufferedReader responseReader;
43.// 处理响应流,必须与服务器响应流输出的编码一致
44.responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), ENCODING_UTF_8));
45.while ((readLine = responseReader.readLine()) != null) {
46.sb.append(readLine).append("\n");
47.}
48.responseReader.close();
49.tv.setText(sb.toString());
50.}
51.} catch (Exception ex) {
52.ex.printStackTrace();
53.}
54.
55.
56.}