您的位置:首页 > 其它

安卓实习期间整理知识点(五)

2015-04-07 19:30 363 查看

安卓实习期间整理知识点(五)

封装要访问的url到方法

String.formt()方法:Returns a localized formatted string, using the supplied format and arguments, using the user’s default locale.

public static String getUserOrderListUrl(Context context,int userid,String listtype) {
// TODO Auto-generated method stub
String param = String.format("user_id=%s&list_type=%s", userid,listtype);
String url = Constant.BASE_URL+ "/api/orders/users/2"+"?"+param;
return url;
}


获取Json array封装成Bean的模板

public static ArrayList<Date> parseToDateList(JSONObject json){
ArrayList<Date> dateList = new ArrayList<Date>();

try {
JSONArray jsonArray = json.getJSONArray("array");
for(int i = 0; i < jsonArray.length(); i++){
Date date = new Date();
date.parseJsonToModel(jsonArray.getJSONObject(i));
dateList.add(date);
}

} catch (JSONException e) {
e.printStackTrace();
}

return dateList;
}


使用AQuery POST方法发送json数据

Entity头域

Content-Length 作用:发送给HTTP服务器数据的长度。

例如: Content-Length: 38

Content-Type 作用:

例如:Content-Type: application/x-www-form-urlencoded

public static final JSONAjaxCallback postJson(Context context, String url,
JSONObject json, ResourceCallback callback) {
if (callback == null) {
Trace.e(TAG, "the callback is null!");
return null;
}
JSONAjaxCallback task = new JSONAjaxCallback(callback);
task.fileCache(false);
task.header("Content-Type", "application/json");

try {
HttpEntity entity = new StringEntity(json.toString());
task.param(AQuery.POST_ENTITY, entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

task.url(url).async(context);
return task;
}


特别注意两者的替代关系

// task.param("JSON", json);

HttpEntity entity = new StringEntity(json.toString());
task.param(AQuery.POST_ENTITY, entity);


android出现的这个问题是因为我们原生框架未明确支持raw数据的添加

导致自己添加的params,都是在默认的x-www-form-urlencoded

开源框架Android Query

AndroidQuery (AQuery) is an open source light-weight library for doing asynchronous tasks and manipulating UI elements in Android. Our goal is to make Android coding simpler, easier, and more fun!

Ajax

AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.

扩展阅读

Android JSON Parsing Tutorial

Android Query

AndroidQuery Future Direction

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