<span style="font-size:24px;">解析数据:</span>
<span style="font-size:24px;">import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.hujing.weibo.object.WeiboItem; import android.os.Handler; import android.os.Message; public class WeiboJsonOperation { public static final int WHAT_OK = 100; public static final int WHAT_NETWORK_ERROR = 102; public static final int WHAT_ERROR = 101; public static final String KEY_ACCESS_TOKEN = "access_token"; public static String VALUE_ACCESS_TOKEN = "2.001DZ_9DKqoJSB024b8331bbc3Q8UD"; /*** 获取当前登录用户及其所关注用户的最新微博的url*/ private final String WEIBO_URI = "https://api.weibo.com/2/statuses/friends_timeline.json"; /** * 获得获取当前登录用户及其所关注用户的最新微博内容 * @param pair * @param handler */ public void getFriendsWeiboLines(final List<NameValuePair> pair,final Handler handler){ new Thread(){ public void run() { HttpUtil util=new HttpUtil(); String result=util.doGet(WEIBO_URI, pair); List<WeiboItem> list=getWeiboItem(result);//解析数据result if(handler!=null){ Message message=handler.obtainMessage(WHAT_OK,list); handler.sendMessage(message); } } }.start(); } /** * 解析数据 * @param result * @return */ private List<WeiboItem> getWeiboItem(String result) { List<WeiboItem> list = new ArrayList<WeiboItem>();// 初始化list JSONObject object; try { object = new JSONObject(result); JSONArray array = object.optJSONArray("statuses"); // Log.i("result", "array--->"+array);//array有值 if (array == null) { return null; } for (int i = 0; i < 10; i++) {// 每次获取10条微博 JSONObject weibo_object = array.optJSONObject(i); // Log.i("result", "weibo_object--->"+weibo_object);//有值 if (weibo_object == null) { return null; } else { OtherUtil oUtil=new OtherUtil(); String time=oUtil.subTime(weibo_object.getString("created_at")); String weiboId=weibo_object.optString("idstr"); String content = weibo_object.optString("text"); String source = weibo_object.optString("source"); int zf_num = weibo_object.optInt("reposts_count"); int pl_num = weibo_object.optInt("comments_count"); int dz_num = weibo_object.optInt("attitudes_count"); JSONObject user = weibo_object.optJSONObject("user");// 获得用户 String user_name = user.optString("screen_name"); String user_image = user.optString("profile_image_url");// 头像地址 String firstImage=weibo_object.optString("thumbnail_pic"); String[] zf_image = {}; String[] image = {}; WeiboItem item = new WeiboItem(weiboId,user_image, user_name, oUtil.subSourceString(source), time, content, image, "", zf_image, zf_num, pl_num, dz_num,firstImage); list.add(item); } } } catch (JSONException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return list; }; }</span>
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.HttpStatus; 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.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * get或者post请求网络的工具 * * @author hj * */ public class HttpUtil { /** * Post请求 * * @param url * 接口地址 * @param pair * 参数和值得集合 * @return 结果,如果没有,返回空 */ public String doPost(String uri, List<NameValuePair> pair) { String result = null; if (pair == null) { pair = new ArrayList<NameValuePair>(); } pair.add(new BasicNameValuePair(WeiboJsonOperation.KEY_ACCESS_TOKEN, WeiboJsonOperation.VALUE_ACCESS_TOKEN));// 添加access_token uri = buildUri(uri, pair); HttpPost post = new HttpPost(uri); try { HttpEntity entity = new UrlEncodedFormEntity(pair, "utf-8"); post.setEntity(entity); result = doExecute(post); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } /** * get请求 * * @param url * 接口地址 * @param pair * 参数和值得集合 * @return 结果,如果没有,返回空 */ public String doGet(String uri, List<NameValuePair> pair) { String result = null;// 返回值 if (pair == null) { pair = new ArrayList<NameValuePair>(); } pair.add(new BasicNameValuePair(WeiboJsonOperation.KEY_ACCESS_TOKEN, WeiboJsonOperation.VALUE_ACCESS_TOKEN));// 添加access_token uri = buildUri(uri, pair); HttpGet get = new HttpGet(uri); result = doExecute(get); return result; } private String doExecute(HttpUriRequest request) { String result = ""; HttpClient client = new DefaultHttpClient(); try { HttpResponse resp = client.execute(request); int code = resp.getStatusLine().getStatusCode(); if (code == HttpStatus.SC_OK) { result = EntityUtils.toString(resp.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 拼接uri * * @param uri * @param pair * @return 拼接完成的uri */ private String buildUri(String uri, List<NameValuePair> pair) { if (pair == null) { return uri; } else { StringBuffer buf = new StringBuffer(); buf.append(uri); int len = pair.size(); for (int i = 0; i < len; i++) { NameValuePair pairItem = pair.get(i); if (i == 0) { buf.append("?"); } else { buf.append("&"); } buf.append(pairItem.getName()); buf.append("="); buf.append(pairItem.getValue()); } return buf.toString(); } } }
jsonOeration.getFriendsWeiboLines(null, handler);
// 如果需要拼接url,如下所示:
List<NameValuePair> pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("id", String.valueOf(urlId)));
jsonOeration.getFriendsWeiboLines(pair, handler);