本文分三个部分:定义接口,封装HttpUtil类,调用。
定义一个接口:
package com.jackie.app_test;
/**
*
* Created by Administrator on 2016/11/8.
*/
public interface HttpCallbackListener {
void onFinish (String response);
void onError (Exception e);
}
包装成一个共同的类HttpUtil(本文核心代码):
package com.jackie.app_test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Administrator on 2016/11/8.
*/
public class HttpUtil {
public static void sendHttpRequestWithHttpURLConnection(final String address, final HttpCallbackListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
//connection.setDoInput(true);
//connection.setDoOutput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
if (listener != null) {
listener.onFinish(response.toString());
}
} catch (Exception e) {
if (listener != null) {
listener.onError(e);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
public static void sendHttpRequestWithHttpClient(final String address, final HttpCallbackListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(address);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
if (listener != null) {
listener.onFinish(response);
}
}
} catch (IOException e) {
if (listener != null) {
listener.onError(e);
}
}
}
}).start();
}
}
运用。
package com.jackie.app_test;
import android.app.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;
/**
* Created by Administrator on 2016/11/8.
*/
public class HttpURLConnectionActivity extends Activity {
private Button btn_send;
private TextView tv_response;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 221){
String string = (String)msg.obj;
tv_response.setText(string);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http);
btn_send = (Button) findViewById(R.id.btn_send);
tv_response = (TextView) findViewById(R.id.tv_response);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String urlAddress = "http://www.ifeng.com";
HttpUtil.sendHttpRequestWithHttpClient(urlAddress, new HttpCallbackListener() {
@Override
public void onFinish(String response) {
Message message = new Message();
message.what = 221;
message.obj = response;
handler.sendMessage(message);
}
@Override
public void onError(Exception e) {
//处理异常,此方法在子线程中运行。
}
});
}
});
}
}
不要忘了权限:
<uses-permission android:name="android.permission.INTERNET"/>