Android的post请求工具,Android客户端post请求服务器端实例

Android客户端请求服务器端的详细解释

1. Android客户端与服务器端通信方式:Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式。

2. 解析服务器端返回数据的解释:

(1).对于服务器端来说,返回给客户端的数据格式一般分为html、xml和json这三种格式。

(2). JSON(Javascript Object Notation)是一种轻量级的数据交换格式,相比于xml这种数据交换格式来说,因为解析xml比较的复杂,而且需要编写大段的代码,所以客户端和服务器的数据交换格式往往通过JSON来进行交换。

3. Android中,用GET和POST访问http资源

(1).客户端向服务器端发送请求的时候,向服务器端传送了一个数据块,也就是请求信息。

(2). GET和POST区别:

A: GET请求请提交的数据放置在HTTP请求协议头(也就是url)中,而POST提交的数据则放在实体数据中,安全性比较高。

B: GET方式提交的数据最多只能有1024字节,而POST则没有此限制。

e8b7b854e3b9fb500638532e2ddf38ab.png

注意:考虑到POST的优势,在Android开发中自己认为最好用POST的请求方式,所以下面自己写了一个小的POST请求的例子。代码如下:

package com.scd.jsondemo.util;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import org.json.JSONException;

import org.json.JSONObject;

public class JsonUtil {

/** 地址 */

private static final String INNER_URL = "http://localhost:8080/index2.jsp";

/** TAG */

private final String TAG = getClass().getSimpleName();

private static final int USER_ID = 1;

/***

* 客户端调用的方法:传递参数向服务器中发送请求

*

* @param userId

* @param userName

* @return

*/

public static JSONObject getData(String userId, String userName) {

int modelId = USER_ID;

List list = new ArrayList();

list.add(new BasicNameValuePair("userId", userId));

list.add(new BasicNameValuePair("userName", userName));

return doPost(modelId, list);

}

/**

* 请求服务器的方法

*

* @param model

* @param paramList

* @return

*/

private static JSONObject doPost(int model, List paramList) {

// 1.创建请求对象

HttpPost httpPost = new HttpPost(INNER_URL);

// post请求方式数据放在实体类中

HttpEntity entity = null;

try {

entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);

httpPost.setEntity(entity);

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

}

// 2.创建客户端对象

HttpClient httpClient = new DefaultHttpClient();

// 3.客户端带着请求对象请求服务器端

try {

// 服务器端返回请求的数据

HttpResponse httpResponse = httpClient.execute(httpPost);

// 解析请求返回的数据

if (httpResponse != null

&& httpResponse.getStatusLine().getStatusCode() == 200) {

String element = EntityUtils.toString(httpResponse.getEntity(),

HTTP.UTF_8);

if (element.startsWith("{")) {

try {

return new JSONObject(element);

} catch (JSONException e) {

e.printStackTrace();

}

}

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值