Http_get_post请求

1. 建立Http连接的步骤:

1.1      获得一个http的连接地址(如:

String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";)

1.2      构造一个URL对象(如:url = new URL(httpurl);)

1.3      使用HttpURLConnection打开一个连接(如:

HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();)

1.4      得到读取的内容(流)(如:InputStream inputStream = httpConnection.getInputStream();)

2.TestHttpGetPostActivity:

  1 import java.io.BufferedReader;
2 import java.io.DataOutputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.net.URLEncoder;
10
11 import android.app.Activity;
12 import android.content.Intent;
13 import android.os.Bundle;
14 import android.view.View;
15 import android.view.View.OnClickListener;
16 import android.widget.Button;
17 import android.widget.TextView;
18
19 public class TestHttpGetPostActivity extends Activity
20 {
21 private Button mButton01;
22 private Button mButton02;
23 private Button mButton03;
24 private String data = null;
25 private TextView mTextView;
26 private Intent intent = new Intent();
27 @Override
28 public void onCreate(Bundle savedInstanceState)
29 {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.main);
32 mButton01 = (Button) findViewById(R.id.get);
33 mButton02 = (Button) findViewById(R.id.post);
34 mButton03 = (Button) findViewById(R.id.nothing);
35 mTextView = (TextView) findViewById(R.id.textview);
36 mButton01.setOnClickListener(listener);
37 mButton02.setOnClickListener(listener);
38 mButton03.setOnClickListener(listener);
39 }
40
41 private OnClickListener listener = new OnClickListener()
42 {
43 @Override
44 public void onClick(View v)
45 {
46 switch (v.getId())
47 {
48 // get请求
49 case R.id.get:
50 try
51 {
52 String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";
53 URL url;
54 url = new URL(httpurl);
55 if (url != null)
56 {
57 // 使用HttpURLConnection打开网络连接
58 HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
59 InputStream inputStream = httpConnection.getInputStream();
60 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
61 while (bufferedReader.readLine() != null)
62 {
63 data += bufferedReader.readLine() + "\n";
64 }
65 // 关闭inputStream
66 inputStream.close();
67 // 关闭http连接
68 httpConnection.disconnect();
69 System.out.println("data = " + data);
70 if (data != null)
71 {
72 intent.putExtra("data", data);
73 intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
74 startActivity(intent);
75 }
76 else
77 {
78 mTextView.setText("data is NULL!");
79 }
80 }
81 }
82 catch (MalformedURLException e)
83 {
84 e.printStackTrace();
85 }
86 catch (IOException e)
87 {
88 mTextView.setText("url is NULL!");
89 e.printStackTrace();
90 }
91 break;
92
93 // post请求
94 case R.id.post:
95 try
96 {
97 String httpurl = "http://192.168.0.68:8090/Test/index.jsp";
98 URL url;
99 url = new URL(httpurl);
100 if (url != null)
101 {
102 // 使用HttpURLConnection打开网络连接
103 HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
104 // 因为是post请求,需要设置成true
105 httpConnection.setDoInput(true);
106 httpConnection.setDoOutput(true);
107 // 设置post请求方式
108 httpConnection.setRequestMethod("POST");
109 // post请求不能使用缓存
110 httpConnection.setUseCaches(false);
111 httpConnection.setInstanceFollowRedirects(true);
112
113 // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded
114 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
115 // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成
116 // 要注意的是connection.getOutputSream会隐含地进行connect.urlConn.connect();
117 // DataOutputSream流
118 DataOutputStream outputStream = new DataOutputStream(httpConnection.getOutputStream());
119 // 要上传的参数
120 String content = "par=" + URLEncoder.encode("this is post request!", "gb2312");
121 // 将要上传的内容写入流中
122 outputStream.writeBytes(content);
123 // 刷新,关闭
124 outputStream.flush();
125 outputStream.close();
126 InputStream inputStream = httpConnection.getInputStream();
127 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
128 while (bufferedReader.readLine() != null)
129 {
130 data += bufferedReader.readLine() + "\n";
131 }
132 // 关闭inputStream
133 inputStream.close();
134 // 关闭http连接
135 httpConnection.disconnect();
136 System.out.println("data = " + data);
137 if (data != null)
138 {
139 intent.putExtra("data", data);
140 intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
141 startActivity(intent);
142 }
143 else
144 {
145 mTextView.setText("data is NULL!");
146 }
147 }
148 }
149 catch (MalformedURLException e)
150 {
151 e.printStackTrace();
152 }
153 catch (IOException e)
154 {
155 mTextView.setText("url is NULL!");
156 e.printStackTrace();
157 }
158 break;
159 case R.id.nothing:
160 try
161 {
162 String httpurl = "http://192.168.0.68:8090/Test/index.jsp";
163 URL url;
164 url = new URL(httpurl);
165 if (url != null)
166 {
167 // 使用HttpURLConnection打开网络连接
168 HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
169 InputStream inputStream = httpConnection.getInputStream();
170 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
171 while (bufferedReader.readLine() != null)
172 {
173 data += bufferedReader.readLine() + "\n";
174 }
175 // 关闭inputStream
176 inputStream.close();
177 // 关闭http连接
178 httpConnection.disconnect();
179 System.out.println("data = " + data);
180 if (data != null)
181 {
182 intent.putExtra("data", data);
183 intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
184 startActivity(intent);
185 }
186 else
187 {
188 mTextView.setText("data is NULL!");
189 }
190 }
191 }
192 catch (MalformedURLException e)
193 {
194 e.printStackTrace();
195 }
196 catch (IOException e)
197 {
198 mTextView.setText("url is NULL!");
199 e.printStackTrace();
200 }
201 break;
202 }
203 }
204 };

3. ShowHtmlContent:

 1 import android.app.Activity;
2 import android.content.Intent;
3 import android.os.Bundle;
4 import android.widget.TextView;
5
6 public class ShowHtmlContent extends Activity
7 {
8 private TextView mTextView;
9
10 @Override
11 public void onCreate(Bundle savedInstanceState)
12 {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.http);
15 mTextView = (TextView) findViewById(R.id.textview);
16 Intent intent = ShowHtmlContent.this.getIntent();
17 String dataString = intent.getStringExtra("data");
18 if (dataString != null)
19 {
20 mTextView.setText(dataString);
21 }
22 else
23 {
24 mTextView.setText("nothing to show!");
25 }
26 }
27 }

4. 效果图:






原文地址:https://www.cnblogs.com/jh5240/p/2229230.html