80,351
社区成员
发帖
与我相关
我的任务
分享
package com.wangy.baidudemo2;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity implements View.OnClickListener {
public static final int SHOW_RESPONSE = 0;
private Button sendRequest;
private TextView responseText;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case SHOW_RESPONSE:
System.out.println("run:===准备打印输出接收到的内容");//---运行时可打印此句
String resp = (String)msg.obj;
if(resp==null){
System.out.println("run:=====response中无内容内容===");
}else{
System.out.println("run:====response中有内容===");//---运行时可打印此句
}
System.out.println(resp);//---运行时打印无输出
responseText.setText(resp);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendRequest = (Button)findViewById(R.id.send_request);
responseText = (TextView)findViewById(R.id.response_text);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View v){
if(v.getId()==R.id.send_request){
sendResponseWithHttpURLConnection();
System.out.println("run:===---点击了按钮---------");
}
}
public void sendResponseWithHttpURLConnection(){
//开启线程来发起网络请求
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
System.out.println("run:=====进入线程===");
try{
URL url = new URL("http://www.baidu.com");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.connect();
InputStream in = connection.getInputStream();
//下面对接收到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder responses = new StringBuilder();
String line;
while((line = reader.readLine())!=null){
responses.append(line);
}
System.out.println("run:====读取到内容===");
Message message = new Message();
message.what = SHOW_RESPONSE;
message.obj = responses.toString();
if(message.obj==null){
System.out.println("run:====obj中无内容内容===");
}else{
System.out.println("run:====obj中有内容===");//---运行时可打印此句
}
handler.sendMessage(message);
}catch(Exception e){
e.printStackTrace();
}finally{
if(connection != null)
connection.disconnect();
System.out.println("run:====关闭连接===");
}
}
}).start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.wangy.baidudemo2.MainActivity">
<Button
android:id="@+id/send_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送请求"/>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/response_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
</LinearLayout>