android中的httpclient使用
2011-05-12 13:35
260 查看
用php搭建了个简单的服务器端,然后在android中使用httpclient来调用.
String httpurl="http://192.168.1.100/http/httptest.php?para=hello"; HttpGet httpGet=new HttpGet(httpurl); HttpParams httpParameters = new BasicHttpParams();// Set the timeout in milliseconds until a connection is established. HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);// Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); try{ HttpResponse httpResponse=httpclient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String strResult=EntityUtils.toString(httpResponse.getEntity()); txtHello.setText(strResult); mWebView.loadUrl(httpurl); } else{ txtHello.setText("no response"); } }catch(IOException e){ e.printStackTrace(); }
刚开始发现在执行httpclient.execute的时候就没有了响应,程序没有继续执行,也没有报告错误,后来一查,需要加入相应的权限才可以:
<uses-permission android:name="android.permission.INTERNET" />
这样就可以正确的返回信息了.
接下来的问题是返回的信息中有中文,在android中显示为乱码,估计是服务器端的问题,于是在php中加入了编码:
header("Content-Type: text/html;charset=utf-8");
可以正常显示了.
String httpurl="http://192.168.1.100/http/httptest.php?para=hello"; HttpGet httpGet=new HttpGet(httpurl); HttpParams httpParameters = new BasicHttpParams();// Set the timeout in milliseconds until a connection is established. HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);// Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); try{ HttpResponse httpResponse=httpclient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String strResult=EntityUtils.toString(httpResponse.getEntity()); txtHello.setText(strResult); mWebView.loadUrl(httpurl); } else{ txtHello.setText("no response"); } }catch(IOException e){ e.printStackTrace(); }
刚开始发现在执行httpclient.execute的时候就没有了响应,程序没有继续执行,也没有报告错误,后来一查,需要加入相应的权限才可以:
<uses-permission android:name="android.permission.INTERNET" />
这样就可以正确的返回信息了.
接下来的问题是返回的信息中有中文,在android中显示为乱码,估计是服务器端的问题,于是在php中加入了编码:
header("Content-Type: text/html;charset=utf-8");
可以正常显示了.
相关文章推荐
- Android使用DefaultHttpClient访问要登录的网络
- android中使用httpclient提交表单
- android端和Struts2服务器端通信,交互信息,参数采用JSON,使用了HttpClient与HttpPost类
- Android-使用HttpClient框架提交数据(get/post)
- android-async-http AsyncHttpClient介绍和使用
- Android使用HttpClient方法和易错问题
- android使用HttpClient实现post get (+1)
- Android使用httpclient访问http
- 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
- android中使用httpclient提交表单
- Android | Android Studio无法使用HttpClient的解决办法
- Android网络请求---AsyncHttpClient的使用
- 【Android基础知识】Apache HttpClient的基本使用
- Android平台HttpClient的使用-手机号码归属地查询
- android HttpURLConnection和HttpClient的使用
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 网络相关系列之一:Android中使用HttpClient发送HTTP请求
- Android中使用httpclient等小结
- Android使用AsyncHttpClient给APP更新版本并安装
- Android中的httpClient使用及JSON格式数据的解析方法