工作 心情2012年11月30日11:15:04
2012-12-05 10:23
134 查看
返回日志列表
工作 心情2012年11月30日11:15:04 2012-11-30 11:26 阅读(0)
赞转载分享评论复制地址编辑
已经是第一篇 | 下一篇:想来想去,还是在...
开通黄钻
工作 心情2012年11月30日11:15:04
今天的任务是完成安卓前端和Java后台之间的交互,具体来说是Json的一些学习,比起昨天有了些进步,相信今天下班前能搞定
。
关于Json的一些知识和常见错误会在完成后补充,至于今天上午的心情,我觉得首先要让自己强大起来,学习能力是个异常重要的技
能。只看不做永远不可能有进步。
经过两天的学习,基本跑通了安卓前端和Java后台之间的联系,邮政豁然开朗的感觉,其实每项技术在未掌握时都会觉得很高深,
掌握之后又会觉得不过如此,也许这就是进步吧。
关于json的一些资料:package com.test.json.util;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
/**
* @ClassName: JsonUtil
* @Description: 解析服务端返回的json数据�?
* @author google
*/
public class JsonUtil {
private static final String TAG = "JSONUtil";
/**
* 获取json内容
*
* @param url
* @return JSONArray
* @throws JSONException
* @throws ConnectionException
*/
public static JSONObject getJSON(String url) throws JSONException,
Exception {
Log.v("JsonUtil HttRrequest", getRequest(url));
return new JSONObject(getRequest(url));
}
/**
* 向api发�?get请求,返回从后台取得的信息�?
*
* @param url
* @return String
*/
protected static String getRequest(String url) throws Exception {
return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
}
/**
* 向api发�?get请求,返回从后台取得的信息�?
*
* @param url
* @param client
* @return String
*/
protected static String getRequest(String url, DefaultHttpClient client)
throws Exception {
String result = null;
int statusCode = 0;
HttpGet getMethod = new HttpGet(url);
Log.d(TAG, "do the getRequest,url=" + url + "");
try {
HttpResponse httpResponse = client.execute(getMethod);
// statusCode == 200 正常
statusCode = httpResponse.getStatusLine().getStatusCode();
Log.d(TAG, "statuscode = " + statusCode);
// 处理返回的httpResponse信息
result = retrieveInputStream(httpResponse.getEntity());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
throw new Exception(e);
} finally {
getMethod.abort();
}
return result;
}
/**
* 处理httpResponse信息,返回String
*
* @param httpEntity
* @return String
*/
protected static String retrieveInputStream(HttpEntity httpEntity) {
int length = (int) httpEntity.getContentLength();
// the number of bytes of the content, or a negative number if unknown.
// If the content length is known but exceeds Long.MAX_VALUE, a negative
// number is returned.
// length==-1,下面这句报错,println needs a message
if (length < 0)
length = 10000;
StringBuffer stringBuffer = new StringBuffer(length);
try {
InputStreamReader inputStreamReader = new InputStreamReader(
httpEntity.getContent(), HTTP.UTF_8);
char buffer[] = new char[length];
int count;
while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
stringBuffer.append(buffer, 0, count);
}
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return stringBuffer.toString();
}
}
package com.test.json.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.util.Log;
public class PostJsonUtil {
public static JSONObject getJsonObject(String url, Map<String, String> map)
throws Exception {
// 构建post给php的参�?
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Set<String> key = map.keySet();
for (Iterator<String> it = key.iterator(); it.hasNext();) {
String s = it.next();
nameValuePairs.add(new BasicNameValuePair(s, map.get(s)));
// System.out.println(map.get(s));
}
// httpUrlConnection();
String result = postHttpData(url, nameValuePairs);
System.out.println(result);
return new JSONObject(result);
}
public static String postHttpData(String url,
List<NameValuePair> nameValuePairs) {
String resultStr = null;
HttpClient httpclient = new DefaultHttpClient();
// 超时请求
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
// 读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
5000);
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response;
response = httpclient.execute(httppost);
resultStr = EntityUtils.toString(response.getEntity());
} catch (UnsupportedEncodingException e) {
Log.d(url, "UnsupportedEncodingException");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d(url, "ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.d(url, "IOException");
e.printStackTrace();
}
return resultStr;
}
public static List<Map<String, String>> GetJsonData(JSONObject jo,
List<String> objectparamelist) {
JSONArray oblist = null;
List<Map<String, String>> maplist = new ArrayList<Map<String, String>>();
try {
oblist = jo.getJSONArray("data");
int length = oblist.length();
for (int i = 0; i < length; i++) { // 遍历JSONArray
Log.d("debugTest", Integer.toString(i));
JSONObject data = oblist.getJSONObject(i);
Map<String, String> map = new HashMap<String, String>();
for (String parame : objectparamelist) {
map.put(parame, data.getString(parame));
}
maplist.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}
return maplist;
}
}
赞转载分享评论复制地址编辑
已经是第一篇 | 下一篇:想来想去,还是在...
个人日记 | 公开 | 原创:Adam
我的热评日志
评论
还没有人发表评论 来坐第一个沙发
发表评论
表情
发表匿名评论(隐身草)
相关文章推荐
- 从现在起每周六的晚上必写一篇技术博客和一篇工作心情博客,以此来提升自己
- 新工作 新心情
- 在关注工作与成就的同时也关注心情与家人,这会让你的心更平衡,工作也显得更有意义。
- 经过这段时间的调整,心情有所好转,还是应该把心思放在工作上,恢复Blog的更新
- 最近心情很郁闷,集中不了精神工作...郁闷!
- 工作了以后的心情变化很大
- [心情]其实我只想要一份稳定有发展潜力的工作,为公司做出最大的贡献之余也丰富我的人生阅历的工作而已从而达到我人生的巅峰
- 心情不爽,贴几张以前工作的照片
- 最近工作心情很不好
- 今天找到了第一份android工作,说说自己的心情,希望一个月之后我能坚持下来 真正的入行
- 今日工作总结和心情
- 如何才能保持一个平稳的心情工作和做事???
- 工作心情
- 忙碌的工作,郁闷的心情
- 【心情】工作之余对工作两年的回顾(内容与标题不符)
- 工作将近两年之当前心情
- 工作半个月来的心情
- 换了一份工作,换了一份心情
- 工作之余,放松心情
- 工作半个月来的心情