您的位置:首页 > 理论基础 > 计算机网络

android 数据存储技术(1/4)android中网络存储

2015-05-05 17:12 176 查看
android中的网络存储说到最终其实也是数据库存储或者文件存储,只不过网络存储是吧本地的数据通过网络的方式存在了服务器的某个文件下或者某个数据库中,这样我们要使用网络存储就必须要会http协议实现网络数据交互;需要熟悉java.net.*,Android.net.*这两个包的内容,并且要在配置文件中设置访问网络权限:

<uses-permission android:name="android.permission.INTERNET" />
 ;

通过HttpGet和HttpPost向服务器提交请求,并从服务器返回结果信息。通过如下3步访问Http资源。

(1)创建HttpGet或者HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。

(2)使用DefaultHttpClient.execute方法发送Http Get或Http Post请求,并返回HttpResponse对象。

(3)通过HttpResponse.getEntity方法返回响应信息,并进行相应的处理。

如果使用HttpPost方法提交Http Post请求,还需要使用HttpPost.setEntity方法设置请求参数。

简单的代码示例:

get方式

String url = "http://blog.csdn.net/a747883500/article/details/45244099";
HttpResponse httpResponse = null;
try{
HttpGet httpGet = new HttpGet(url);
httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{

String result = EntityUtils.toString(httpResponse
.getEntity());

}
} catch (Exception e) {

}post方式:
String url = "http://blog.csdn.net/a747883500/article/details/45244099";
HttpResponse httpResponse = null;
try{
HttpPost httpPost = new HttpPost(url);
List<> params = new ArrayList<>();
params.add(目标数据键,目标数据值);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String result = EntityUtils.toString(httpResponse
.getEntity());

}
} catch (Exception e) {

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  存储 数据存储 网络