前言
由于接收原来的老项目并进行维护,之前的http请求是使用Apache Jakarta Common下的子项目HttpClient,因此使用了 List<NameValuePair> 方式来创建参数队列,然后通过一个字符串处理的工具类,生成的post请求体格式大致如:
a=b&c=d&e=f
但是由于服务器api变更,需要将请求体的格式变成json格式,因此上面的请求参数要变成:
{"a":"b","c":"d","e":"f"}
但是如果将List<NameValuePair>直接通过Jackson或者Gson等第三方工具的转json的开源类库去转换为json格式的话,转出来的json格式如:
[{"name":"a","value":"b"},{"name":"c","value":"d"},{"name":"e","value":"f"}]
如下面代码所示:
public class RequestParams {
/**请求地址*/
public String url;
/**请求参数集合*/
public List<NameValuePair> params;
}
/**
* 帐号登录
*
* @param userName 账号名
* @param password 密码
* @param days 记住密码天数:days>0定期超时;days=-2永不失效;
* days=-1永久记住密码(30天没有操作则失效)
* @param deviceId 账户在设备上的唯一标识符(机器码或mac地址)
* @param appId 应用id
* @return
*/
public static RequestParams loginParams(String userName, String password, int days, String deviceId, int appId) {
RequestParams params = new RequestParams();
params.url = LOGIN_URI;
params.params = new ArrayList<NameValuePair>();
params.params.add(new BasicNameValuePair("userName", userName));
params.params.add(new BasicNameValuePair("password", password));
params.params.add(new BasicNameValuePair("days", String.valueOf(days)));
params.params.add(new BasicNameValuePair("deviceId", deviceId));
params.params.add(new BasicNameValuePair("appId", String.valueOf(appId)));
params = addcommonParams(params, LOGIN_URI, appId);
return params;
}
在请求的时候,将List<NameValuePair> params取出来,然后使用JSONUtil工具类转换成json格式,代码如下所示:
String request=JSONUtil.toJSON(requestParams.params);
其中换成json格式的工具类JSONUtil,该工具类使用了jackjson的第三方json解析库,代码如下所示:
import android.util.Log;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
/**
* JSON转换工具类
*
* @author OuyangPeng
* @version [1.0.0.0, 2016-3-23]
*/
public class JSONUtil {
private static String TAG = JSONUtil.class.getName();
private static ObjectMapper mapper = new ObjectMapper();
static {
//对于为null的字段不进行序列化
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
//对于未知属性不进行反序列化
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//无论对象中的值只有不为null的才进行序列化
mapper.setSerializationInclusion(Include.NON_NULL);
}
/**
* 把对象转化成json字符串
*
* @param obj
* @return
*/
public static String toJSON(Object obj) {
if (obj == null) {
return null;
}
Writer write = new StringWriter();
try {
mapper.writeValue(write, obj);
} catch (JsonGenerationException e) {
Log.e(TAG, e.toString() + obj);
} catch (JsonMappingException e) {
Log.e(TAG, e.toString() + obj);
} catch (IOException e) {
Log.e(TAG, e.toString() + obj);
}
return write.toString();
}
/**
* JSON字符串转成对象
*
* @param jsonStr
* @param classType
* @return
*/
public static <T> T fromJSON(String jsonStr, Class<T> classType) {
if (isEmptyOrNull(jsonStr)) {
return null;
}
T t = null;
try {
t = mapper.readValue(jsonStr.getBytes("utf-8"), classType);
} catch (JsonParseException e) {
Log.e(TAG, e.toString() + ", jsonStr:" + jsonStr + ", classType:" + classType.getName());
} catch (JsonMappingException e) {
Log.e(TAG, e.toString() + ", jsonStr:" + jsonStr + ", classType:" + classType.getName());
} catch (IOException e) {
Log.e(TAG, e.toString() + ", jsonStr:" + jsonStr + ", classType:" + classType.getName());
}
return t;
}
/**
* JSON字符串转化成集合
*
* @param jsonStr
* @return
*/
public static <T> T toCollection(String jsonStr, Class<?> collectionClass, Class<?>... elementClasses) {
if (isEmptyOrNull(jsonStr)) {
return null;
}
T t = null;
try {
t = mapper.readValue(jsonStr.getBytes("utf-8")
, mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses));
} catch (JsonParseException e) {
e.printStackTrace();
Log.e(TAG, e.toString() + ", jsonStr:" + jsonStr);
} catch (JsonMappingException e) {
e.printStackTrace();
Log.e(TAG, e.toString() + ", jsonStr:" + jsonStr);
} catch (IOException e) {
Log.e(TAG, e.toString() + ", jsonStr:" + jsonStr);
}
return t;
}
public static Object get(String jsonStr, String key) {
Object obj = null;
try {
JSONObject jsonObj = new JSONObject(jsonStr);
obj = jsonObj.get(key);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
/**
* 判断字符串是否为null或者""
*/
public static boolean isEmptyOrNull(String content) {
if (content == null || content.equals("")) {
return true;
}
return false;
}
}
使用上面的代码将List<NameValuePair> params请求参数集合转换为json后,(不标准)格式如下所示:
[{"name":"userName","value":"13911002200"},{"name":"password","value":"123456"},{"name":"days","value":"-1"},{"name":"deviceId","value":"02:00:00:00:00:00"},{"name":"appId","value":"11"},{"name":"mac","value":"02:00:00:00:00:00"},{"name":"timestamp","value":"2016-03-26 10:24:35"},{"name":"sign","value":"b2873caa5735d16535ff827ee2701cd1"}]
好吧,这样的格式完全不是标准的json格式,服务器那边需要的(标准)json格式是:
{"deviceId":"02:00:00:00:00:00","password":"123456","days":"-1","sign":"b2873caa5735d16535ff827ee2701cd1","mac":"02:00:00:00:00:00","timestamp":"2016-03-26 10:24:35","appId":"11","userName":"13911002200"}
另外一个请求参数队列转后的json格式如下,(不标准):
[{"name":"accountId","value":"2384403"},{"name":"password","value":"123456"},{"name":"ticket","value":"{"accountId":"2384403","appId":"11","loginName":"1302200","userName":"U12384403","email":"","mobilePhone":"1302200","timestamp":"2016-03-26","maxAge":-1,"ip":"","sign":"4109c59eff52a924e74be3623ecab27d","deviceId":"02:00:00:00:00:00"}"},{"name":"appId","value":"11"},{"name":"mac","value":"02:00:00:00:00:00"},{"name":"timestamp","value":"2016-03-26 10:26:01"},{"name":"sign","value":"22070f51197c15d256764a2de499ef85"}]
服务器那边需要的另外一个请求参数队列转后的(标准)json格式如下所示:
{"timestamp":"2016-03-26 10:26:01","password":"123456","appId":"11","ticket":"{"accountId":"2384403","appId":"11","loginName":"1302200","userName":"U12384403","email":"","mobilePhone":"1302200","timestamp":"2016-03-26","maxAge":-1,"ip":"","sign":"4109c59eff52a924e74be3623ecab27d","deviceId":"02:00:00:00:00:00"}","sign":"22070f51197c15d256764a2de499ef85","accountId":"2384403","mac":"02:00:00:00:00:00"}
好的,现在来说一说解决方法:
step1:将List<NameValuePair> params进行遍历然后丢在同一个Map集合中去
step2:使用JONUtil对Map集合进行转换成json格式字符串
代码如下所示:
/*** 将请求参数转换为json格式*/
Map map=new HashMap();
for (int i=0;i<requestParams.params.size();i++){
NameValuePair nameValuePair=requestParams.params.get(i);
map.put(nameValuePair.getName(),nameValuePair.getValue());
}
String request=JSONUtil.toJSON(requestParams.params);
String jsonRequest=JSONUtil.toJSON(map);
LogUtil.d(TAG, "发送给服务器的内容为:加密前的data::" + request);
LogUtil.d(TAG, "发送给服务器的内容为:加密前的 json data::" + jsonRequest);
打印出来的日志如下所示:
发送给服务器的内容为:加密前的data::[{"name":"userName","value":"13011002200"},{"name":"password","value":"123456"},{"name":"days","value":"-1"},{"name":"deviceId","value":"02:00:00:00:00:00"},{"name":"appId","value":"11"},{"name":"mac","value":"02:00:00:00:00:00"},{"name":"timestamp","value":"2016-03-26 10:24:35"},{"name":"sign","value":"b2873caa5735d16535ff827ee2701cd1"}]
发送给服务器的内容为:加密前的 json data::{"deviceId":"02:00:00:00:00:00","password":"123456","days":"-1","sign":"b2873caa5735d16535ff827ee2701cd1","mac":"02:00:00:00:00:00","timestamp":"2016-03-26 10:24:35","appId":"11","userName":"13011002200"}
发送给服务器的内容为:加密前的data::[{"name":"accountId","value":"2384403"},{"name":"password","value":"123456"},{"name":"ticket","value":"{\"accountId\":\"2384403\",\"appId\":\"11\",\"loginName\":\"130***2200\",\"userName\":\"U12384403\",\"email\":\"\",\"mobilePhone\":\"130***2200\",\"timestamp\":\"2016-03-26\",\"maxAge\":-1,\"ip\":\"\",\"sign\":\"4109c59eff52a924e74be3623ecab27d\",\"deviceId\":\"02:00:00:00:00:00\"}"},{"name":"appId","value":"11"},{"name":"mac","value":"02:00:00:00:00:00"},{"name":"timestamp","value":"2016-03-26 10:26:01"},{"name":"sign","value":"22070f51197c15d256764a2de499ef85"}]
发送给服务器的内容为:加密前的 json data::{"timestamp":"2016-03-26 10:26:01","password":"123456","appId":"11","ticket":"{\"accountId\":\"2384403\",\"appId\":\"11\",\"loginName\":\"130***2200\",\"userName\":\"U12384403\",\"email\":\"\",\"mobilePhone\":\"130***2200\",\"timestamp\":\"2016-03-26\",\"maxAge\":-1,\"ip\":\"\",\"sign\":\"4109c59eff52a924e74be3623ecab27d\",\"deviceId\":\"02:00:00:00:00:00\"}","sign":"22070f51197c15d256764a2de499ef85","accountId":"2384403","mac":"02:00:00:00:00:00"}