Http-创新互联
HttpClient有两个实现子类,一个是AndroidHttpClient,一个是Default
HttpClient;AndroidHttpClient继承自DefaultHttpClient,是针对Android
设备的对某些参数进行了默认设置。
DefaultHttpClient client = new DefaultHttpClient();
可以对client设置参数(HttpParams),如连接超时
HttpParams params = new BasicHttpParams();
params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT,10*1000);
client.setParams(params);
创新互联公司是专业的永泰网站建设公司,永泰接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行永泰网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!1)对于get请求
HttpGet request = new HttpGet("http://www.renren.com?email="
+"zhangsan"+"&password="+"123");
HttpResponse response= client.execute(request);
int statusCode = respose.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK){
String str= EntityUtils.toString(response.getEntity());
System.out.println(str);
}
2)对于post请求
HttpPost request = new HttpPost("http://www.renren.com");
List pairs = new ArrayList();
pairs.add(new BasicNameValuePair("email","zhangsan@126.com"));
pairs.add(new BasicNameValuePair("password","123"));
UrlEncodedFormEntity formEntity= new UrlEncodedFormEntity(pairs);
//或者指定编码,如下
//UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs,"UTF-8");
request.setEntity(formEntity);
HttpResponse response= client.execute(request);
int statusCode = respose.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK){
String str= EntityUtils.toString(response.getEntity());
System.out.println(str);
}
EntityUtils是一个工具类,所以它的方法为静态方法,它用来解析实体。提供的方法有
EntityUtils.toString(HttpEntity entity)-->String 作用将response的entity
按照默认编码转为字符串
EntityUtils.toString(HttpEntity entity, String defaultCharset) -->String
按照指定编码转为字符串
EntityUtils.toByteArray(HttpEntity entity)-->byte[] 作用是将response
的entity转为byte数组,如:可以处理图片
如下边例子:
HttpEntity entity = response.getEntity();
byte[] bs = EntityUtils.toByteArray(entity);
Bitmap bitmap= BitmapFactory.decodeByteArray(bs, 0, bs.length);
得到Bitmap就可以设置到ImageView中了。
注意:需要联网权限
2.注意
127.0.0.1 和 localhost都表示android手机自己本身的地址。
3.HttpUrlConnection综述
HttpUrlConnection可以处理get请求也可以处理post请求。
get方式介绍:
使用方式:
step1.
URL url = new URL("http://119.97.113.180:8080/test/t?username=zhangsan&password=123");
step2.通过URL创建HttpUrlConnection对象
HttpUrlConnectioin connection = url.openCennection();
step3.得到输入流。读取服务器返回的数据。
step4.关闭流
step5.connection.disconnect();断开连接。
String httpUrl = "http://119.97.113.180:8080/test/t?username=zhangsan&password=123"
URL url= new URL(httpUrl);
if (url != null){
HttpURLConnection urlConn= (HttpURLConnection) url
.openConnection();
InputStreamReader in= new InputStreamReader(urlConn
.getInputStream());
BufferedReader buffer= new BufferedReader(in);
String inputLine= null;
while ((inputLine = buffer.readLine()) != null) {
resultData+= inputLine + "
";
}
in.close();
urlConn.disconnect();
post方式:
step1.创建URL对象
URL url = new URL("http://192.168.1.108:8080/test/t");
step2.得到HttpURLConnection对象
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
step3.对connection对象进行设置
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");//注意:这里必须为大写urlConn.setUseCaches(false);
step4.调用connection的connect()方法。
step5.得到输出流,输出传给服务器的参数,然后关闭流。
step6.得到输入流,读取服务器响应的数据,然后关闭流。
step7.connection.disconnect();
String httpUrl = "http://192.168.1.108:8080/test/t";
String resultData= "";
URL url= new URL(httpUrl);
if (url != null) {
HttpURLConnection urlConn= (HttpURLConnection) url
.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false);
urlConn.connect();
DataOutputStream out= new DataOutputStream(
urlConn.getOutputStream());
// 用户名和密码 String content = "username=" + URLEncoder.encode("******")
+ "&password=" + "*******";
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader= new BufferedReader(
new InputStreamReader(urlConn.getInputStream()));
String inputLine= null;
while ((inputLine = reader.readLine()) != null) {
resultData+= inputLine + "
";
}
reader.close();
urlConn.disconnect();
}
4.WebView控件的使用WebView用来加载网页
step0.manifest.xml增加联网权限
step1.在layout中定义
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
step2.在Activity中
wv = (WebView) findViewById(R.id.wv);
wv.loadUrl("http://www.baiud.com");
step3.保证每次都使用WebView打开连接。
wv.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;//一定要为true
}
});
网页标题:Http-创新互联
本文地址:http://pwwzsj.com/article/cdheoc.html