您的位置:首页 > 其它

工作 心情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

我的热评日志

评论

还没有人发表评论  来坐第一个沙发

发表评论

表情

发表匿名评论(隐身草)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: