android通过 HttpClient 请求服务器端例子
? ? ? // http地址 ?
? ? ? ? String httpUrl = "http://192.168.1.110:8080/my.jsp?method=testGet"; ?
? ? ? ? //HttpGet连接对象 ?
? ? ? ? HttpGet httpRequest = new HttpGet(httpUrl); ?
? ? ? ? ?//取得HttpClient对象 ?
? ? ? ? ? ? HttpClient httpclient = new DefaultHttpClient(); ?
? ? ? ? ? ? //请求HttpClient,取得HttpResponse ?
? ? ? ? ? ? HttpResponse httpResponse = httpclient.execute(httpRequest); ?
? ? ? ? ? ? //请求成功 ?
? ? ? ? ? ? if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? //取得返回的字符串 ?
? ? ? ? ? ? ? ? String strResult = EntityUtils.toString(httpResponse.getEntity()); ?
? ? ? ? ? ? ? ?//strResult ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ?//请求失败
? ? ? ? ? ? } ?
? ? ? ? }?
1 楼 酷的飞上天空 4 小时前 请求完成后注意释放HttpClient建立的链接