HttpURLConnection 用法
- Socket
- HTTP
- HttpURLConnection
- HttpClient
URL url = new URL("www.baidu.com");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
public class MainActivity extends Activity implements OnClickListener {
private Button sendRequest;
private TextView responseText;
@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);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.send_request) {
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
try {
URL url = new URL("www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setRequestMethod("GET");
String line = null;
StringBuilder response = new StringBuilder();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
response.append(line);
Log.e("MainActivity", line);
}
responseText.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
}
}
}).start();
}
}
- 写网址,也就是URL时,需要加上协议名,也就是"http://"
- 上面的代码如果真的运行,文本框里是不会显示任何内容的,因为子线程中无法对 UI 进行操作
public class MainActivity extends Activity implements OnClickListener {
private Button sendRequest;
private TextView responseText;
public static final int SHOW_RESPONSE = 0;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case SHOW_RESPONSE:
String response = (String)msg.obj;
responseText.setText(response);
break;
default:
break;
}
}
};
@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);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.send_request) {
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
try {
URL url = new URL("http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setRequestMethod("GET");
String line = null;
StringBuilder response = new StringBuilder();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
response.append(line);
Log.e("MainActivity", line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
}
}
}).start();
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。