用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");

可以正常显示了.

更多相关文章

  1. android+mina 开发环境搭建
  2. Android(安卓)UI开源软件(三)
  3. android 通过经纬度获取地址信息
  4. 跟踪Android(安卓)callback 调用堆栈
  5. Android平台开发-WIFI function porting-WIFI功能移植
  6. QT 与 android java socket通讯 中文字符编码问题 解决
  7. Android(安卓)Market 分析【安智市场】
  8. 禁止应用获取手机信息
  9. 如何查看Android的ANR(无响应)异常

随机推荐

  1. php使用openssl进行Rsa长数据加密(117)解
  2. php和apache的关系和作用()
  3. php中static 静态变量和普通变量的区别
  4. 解析错误:语法错误,第12行/home/public_htm
  5. 如何将数据从MySQL表放入谷歌图表API?
  6. ajax - 使用onload和onunload开始和结束
  7. PHP实现快速排序
  8. PHP文件锁同步实例
  9. weiphp插件开发注意
  10. 《细说PHP》第一章 LAMP网站的构建---读