在Android 中发送HTTP请求的方式一般有两种,HttpURLConnection和HttpClient,下面介绍的是HttpURLConnection的使用方法.
1.定义xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button">
</Button>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="sdsd"
android:id="@+id/response_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
2.Activity文件
package com.emeet.one.elite.activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.emeet.one.elite.R;
//import com.emeet.one.elite.service.HttpDownloader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoaderDemo extends BaseActivity {
// HttpDownloader downloader;
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:
String response = (String) msg.obj;
//在这里进行UI操作,将结果显示到屏幕上
responseText.setText(response);
}
}
};
class MyListener implements View.OnClickListener{
@Override
public void onClick(View view) {
//处理逻辑
sendRequestWithHttpURLConnect();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_downloader);
sendRequest = (Button) this.findViewById(R.id.button);
responseText = (TextView) this.findViewById(R.id.response_text);
// sendRequest.setOnClickListener(this);
sendRequest.setOnClickListener(new MyListener());
}
public void onClick(View v) {
if (v.getId() == R.id.button) {
sendRequestWithHttpURLConnect();
}
}
private void sendRequestWithHttpURLConnect() {
//开启线程来发起网络请求
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("https://blog.csdn.net/qq_42866164");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
//下面对获取得到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
response.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
//将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
}catch (Exception e ){
e.printStackTrace();
}finally {
if (connection != null){
connection.disconnect();
}
}
}
}).start();
}
}
3. 在AndroidManifest中注册Activity,并声明网络权限
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".activity.DownLoaderDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>