标签:
这个很不错的第三方开源类库,针对Android开发中发送http请求的。
1 import com.loopj.android.http.*; 2 3 public class TwitterRestClient { 4 private static final String BASE_URL = "http://api.twitter.com/1/"; 5 6 private static AsyncHttpClient client = new AsyncHttpClient(); 7 8 public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { 9 client.get(getAbsoluteUrl(url), params, responseHandler); 10 } 11 12 public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { 13 client.post(getAbsoluteUrl(url), params, responseHandler); 14 } 15 16 private static String getAbsoluteUrl(String relativeUrl) { 17 return BASE_URL + relativeUrl; 18 } 19 }
1 import org.json.*; 2 import com.loopj.android.http.*; 3 4 class TwitterRestClientUsage { 5 public void getPublicTimeline() throws JSONException { 6 TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() { 7 @Override 8 public void onSuccess(int statusCode, Header[] headers, JSONObject response) { 9 // If the response is JSONObject instead of expected JSONArray 10 } 11 12 @Override 13 public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) { 14 // Pull out the first event on the public timeline 15 JSONObject firstEvent = timeline.get(0); 16 String tweetText = firstEvent.getString("text"); 17 18 // Do something with the response 19 System.out.println(tweetText); 20 } 21 }); 22 } 23 }
标签:
原文地址:http://www.cnblogs.com/chenlong-50954265/p/4496702.html