1、activity开启线程访问http
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
String url = HttpUtil.BASE_URL + "queryYEServlet";
ye = HttpUtil.queryStringForPost(url,sfz);
yeView.setText(ye);
Log.i("ye", ye);
}catch(Exception e){
Log.i("Register",e.toString());
}
}
}).start();
2、post访问方式
第一种:
public static String queryStringForPost(String url, String sfz) {
// 第一步,创建HttpPost对象
HttpPost httpPost = new HttpPost(url);
String result = null;
HttpResponse response= null;
// 设置HTTP POST请求参数必须用NameValuePair对象
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sfz", sfz));
Log.i("params", params + "");
try {
// 设置httpPost参数
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpPost.addHeader("Content-Type", "application/xml;charset=utf-8");
Log.i("httpPost", httpPost + "");
// 第二步,使用execute方法发送HTTP POST请求,并返回HttpResponse对象
//response = HttpUtil.getHttpResponse(httpPost);
response = new DefaultHttpClient().execute(httpPost);
Log.i("response", response.toString());
Log.i("status", response.getStatusLine().getStatusCode() + "");
// 判断是否请求成功
if (response.getStatusLine().getStatusCode() == 200) {
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!!!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "IO网络异常!";
return result;
}
return null;
}
url为访问地址,不带参数
第二种:public static Map<String, Object> getConnectionData(String data,
String urlSource) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
String result = null;
boolean network = false;
URL url;
HttpURLConnection conn = null;
InputStream inStream = null;
ByteArrayOutputStream out = null;
byte[] entity = null;
if (data != null)
entity = data.getBytes();
try {
url = new URL(urlSource);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setDoOutput(true);// 允许对外输出数据
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
// 设置HTTP请求头
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
if (entity != null)
conn.setRequestProperty("Content-Length",
String.valueOf(entity.length));
conn.setRequestProperty("Connection", "Keep-Alive");
// 发送参数
if (entity != null) {
DataOutputStream outStream1 = new DataOutputStream(
conn.getOutputStream());
outStream1.write(entity);// 把参数发送出去
outStream1.flush();
outStream1.close();
}
// 请求成功
if (conn.getResponseCode() == 200) {
network = true;
inStream = conn.getInputStream();
out = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = inStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
result = out.toString();
}
} catch (Exception e) {
Log.i("WebConnection-getConnectionData()", e.toString());
} finally {// 关闭
if (out != null)
out.close();
if (inStream != null)
inStream.close();
if (conn != null)
conn.disconnect();
}
map.put("result", result);
map.put("network", network);
return map;
}
data格式如“name=”+name+"&age="+age,urlSource为访问IP
3、get访问方式
public static String queryStringForGet(String url) {
// 获得HttpGet对象
HttpGet request = HttpUtil.getHttpGet(url);
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if (response.getStatusLine().getStatusCode() == 200) {
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}