网络通信已经学了两天了,因为还是学习阶段,并没有做到多么高端,只是初步了解一些方法的使用。
开始第一个学习任务是用HttpURLConnection请求一下百度的XML文件。
先放一下代码:
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case SHOW_RESPONSE:
String response = (String)msg.obj;
responseText.setText(response);
}
}
};这段代码的大致作用是把获取到的信息存储在Message上,Handler是完成这一活动的方法(因为初学,这是我个人对于代码的认知)
sendRequestWithHttpURLConnection();
这个方法是设置具体的接口(URL)和参数(out.write),而参数的设置需要new一个输出流的实体对象:
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
然后用实体对象out调用write方法实现传参;
值得提的是下面这一段:
connection = (HttpURLConnection) url.openConnection();//这是开启连接方法
connection.setRequestMethod("POST");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded;charset=utf-8");
对这些东西,我的个人理解可以把他们当成XML文件里面的类似于宽、高、距离父容器边缘的距离之类的属性。其实他们原本就是属性,不过是网络请求的属性(请求时间超时,等待时间超时...)只要用网络请求实体对象connection调用一下相关的方法就可以进行设置。
我深知小白不见源码不下笔的苦楚,把完整的源码粘贴出来:
public class HttpDamo extends AppCompatActivity implements View.OnClickListener {
public static final int SHOW_RESPONSE = 0;
private Button sendRequest;
private TextView responseText;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case SHOW_RESPONSE:
String response = (String)msg.obj;
responseText.setText(response);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_damo);
sendRequest = (Button)findViewById(R.id.send_request);
responseText = (TextView)findViewById(R.id.request_text);
sendRequest.setOnClickListener(this);
}
public void onClick (View v){
if (v.getId()==R.id.send_request){
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL
("http:baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded;charset=utf-8");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write("参数".getBytes());
out.flush();
out.close();
InputStream in = connection.getInputStream();
BufferedReader resder = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line=resder.readLine()) !=null){
response.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
message.obj = response.toString();
handler.sendMessage(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (connection != null){
connection.disconnect();
}
}
}
}).start();
}
}
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦