安卓如何访问云服务器地址?使用安卓程序连接到云服务器,用httpclient或者okhttp 原理都是一样,发起http请求传递数据,解析结果。这里我们根据百度知道网友的介绍,举例一下 httpclient 的用法:
1. get 方式传递参数
//先将参数放入list,再对参数进行url编码
list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair>();
params.add(new basicnamevaluepair(param1, 数据)); // 增加参数1
params.add(new basicnamevaluepair(param2, value2));// 增加参数2
string param = urlencodedutils.format(params, utf-8);// 对参数编码
string baseurl = 服务器接口完整url;
httpget getmethod = new httpget(baseurl + ? + param);// 将url与参数拼接
httpclient httpclient = new defaulthttpclient();
try {httpresponse response = httpclient.execute(getmethod); // 发起get请求
log.i(tag, rescode = + response.getstatusline().getstatuscode()); // 获取响应码
log.i(tag, result = + entityutils.tostring(response.getentity(), utf-8));// 获取服务器响应内容
} catch (clientprotocolexception e) {e.printstacktrace();} catch (ioexception e)
{e.printstacktrace();}2. post方式 方式传递参数
//和get方式一样,先将参数放入list
params = new linkedlist<basicnamevaluepair>();
params.add(new basicnamevaluepair(param1, post方法));// 增加参数1
params.add(new basicnamevaluepair(param2, 第二个参数));// 增加参数2
try {httppost postmethod = new httppost(baseurl);// 创建一个post请求
postmethod.setentity(new urlencodedformentity(params, utf-8)); // 将参数填入post entity中
httpresponse response = httpclient.execute(postmethod); //执行post方法
log.i(tag, rescode = + response.getstatusline().getstatuscode()); // 获取响应码
log.i(tag, result = + entityutils.tostring(response.getentity(), utf-8)); // 获取响应内容
} catch (unsupportedencodingexception e) {e.printstacktrace();
} catch (clientprotocolexception e) {e.printstacktrace();}
catch (ioexception e) {e.printstacktrace();}以上是关于安卓如何访问云服务器地址的分享,西部数码网站提供多类型、高性价的云服务器产品,云主机购买链接 https://www.west.cn/cloudhost/
成都阿里云服务器租用华为云服务器可以按小时收费吗如何解决php mail收不到的问题icp备案是否现场核查虚拟主机续费多少钱一年百度云服务器挂机域名注册哪里比较好 注册域名要注意什么微信开放平台开发(一)