版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lsf1025995457/article/details/52589634
简单的随手记,适合初学者使用,现在网络请求比较流行使用OKHttp,后期博客会写到如何使用。
在AndroidManifest加入以下权限
<uses-permission android:name="android.permission.INTERNET" />
Http get请求
public class HttpGetThread extends Thread {
private Handler handler;
private String httpUrl;
private int mWhat;
public static final int ERROR = 404;
public static final int SUCCESS = 200;
public HttpGetThread(Handler handler, String httpUrl) {
super();
this.handler = handler;
this.httpUrl = httpUrl;
mWhat = SUCCESS;
}
public HttpGetThread(Handler handler, String httpUrl, int what) {
super();
this.handler = handler;
this.httpUrl = httpUrl;
mWhat = what;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
// HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(httpUrl);
String result = null;
HttpResponse response = new DefaultHttpClient().execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, HTTP.UTF_8);
handler.sendMessage(Message.obtain(handler, mWhat, result)); // 请求成功
} else {
result = "请求失败" + response.getStatusLine().getStatusCode(); // 请求失败
// 404 - 未找到
handler.sendMessage(Message.obtain(handler, Config.ERROR,
result));
}
} catch (Exception e) {
e.printStackTrace();
handler.sendMessage(Message.obtain(handler, Config.ERROR, "异常退出"));
}
super.run();
}
}
http post请求
public class HttpPostThread extends Thread {
private Handler handler;
private String httpUrl;
private List<NameValuePair> valueList;
private int mWhat;
public static final int ERROR = 404;
public static final int SUCCESS = 200;
public HttpPostThread(Handler handler, String httpUrl,
List<NameValuePair> list, int what) {
super();
this.handler = handler;
this.httpUrl = httpUrl;
this.valueList = list;
this.mWhat = what;
}
public HttpPostThread(Handler handler, String httpUrl,
List<NameValuePair> list) {
super();
this.handler = handler;
this.httpUrl = httpUrl;
this.valueList = list;
this.mWhat = SUCCESS ;
}
@Override
public void run() {
// TODO Auto-generated method stub
String result = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(httpUrl);
httpPost.setEntity(new UrlEncodedFormEntity(valueList, HTTP.UTF_8));
// 请求超时
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
HttpResponse response = httpclient.execute(httpPost);
// HttpStatus.SC_OK表示连接成功
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, HTTP.UTF_8);
if (handler != null) {
handler.sendMessage(Message.obtain(handler, mWhat, result)); // 请求成功
} else {
}
} else {
result = "请求失败" + response.getStatusLine().getStatusCode(); // 请求失败
// HttpEntity entity = response.getEntity();
// String str = EntityUtils.toString(entity, HTTP.UTF_8);
MyLog.println(""+result);
// 404 - 未找到
if (handler != null) {
handler.sendMessage(Message.obtain(handler, Config.ERROR,
result));
}
}
} catch (Exception e) {
e.printStackTrace();
if (handler != null) {
result = "请求失败 Exception";
handler.sendMessage(Message.obtain(handler, Config.ERROR,
result));
}
}
super.run();
}
public static String HttpAsynPost(String AUrl, List<NameValuePair> ValueList) {
String result = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(AUrl);
httpPost.setEntity(new UrlEncodedFormEntity(ValueList, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);
// HttpStatus.SC_OK表示连接成功
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, HTTP.UTF_8);
} else {
result = "请求失败" + response.getStatusLine().getStatusCode(); // 请求失败
}
} catch (Exception e) {
e.printStackTrace();
result = "请求失败,网络异常";
}
return result;
}
}
实例:
public class MainActivity extends Activity {
private Button btn, btn1;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
initData();
}
private void initView() {
btn = (Button) findViewById(R.id.btn);
btn1 = (Button) findViewById(R.id.btn1);
}
private void initListener() {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
HttpPostThread thread = new HttpPostThread(mHandler, "http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0", list, 200);
thread.start();
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HttpGetThread thread = new HttpGetThread(mHandler, "http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0", 200);
thread.start();
}
});
}
private void initData() {
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String result = (String) msg.obj;
switch (msg.what) {
case 200:
//请求成功
Log.e("TAG", "返回参数===" + result);
break;
case 404:
//请求失败
Log.e("TAG", "请求失败!");
break;
}
}
};
}
}