編輯:關於Android編程
導包:com.loopj.android android-async-http1.4.5
import com.loopj.android.http.*;創建一個AsyncHttpClient 對象並發送一個請求:
client.get(http://www.google.com, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is 200 OK
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is 4XX (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
import com.loopj.android.http.*;
public class TwitterRestClient {
private static final String BASE_URL = http://api.twitter.com/1/;
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
import org.json.*;
import com.loopj.android.http.*;
class TwitterRestClientUsage {
public void getPublicTimeline() throws JSONException {
TwitterRestClient.get(statuses/public_timeline.json, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Pull out the first event on the public timeline
JSONObject firstEvent = timeline.get(0);
String tweetText = firstEvent.getString(text);
// Do something with the response
System.out.println(tweetText);
}
});
}
}
PersistentCookieStore ,這個類是Apache HttpClient CookieStore 接口的實現,它可以自動將cookies保存到SharedPreferences 。 如果你需要使用cookie保持認證會話,這將是特別重要的,因為即使用戶關掉了應用仍然可以登錄狀態。 首先,創建一個AsyncHttpClient對象:
AsyncHttpClient myClient = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(this); myClient.setCookieStore(myCookieStore);
BasicClientCookie newCookie = new BasicClientCookie(cookiesare, awesome); newCookie.setVersion(1); newCookie.setDomain(mydomain.com); newCookie.setPath(/); myCookieStore.addCookie(newCookie);
RequestParams params = new RequestParams(); params.put(key, value); params.put(more, data);2.創建一個帶有一對參數的RequestParams
RequestParams params = new RequestParams(single, value);3.創建一個帶有Map的RequestParams
HashMap詳情請參考:RequestParams JavadocparamMap = new HashMap (); paramMap.put(key, value); RequestParams params = new RequestParams(paramMap);
InputStream myInputStream = blah; RequestParams params = new RequestParams(); params.put(secret_passwords, myInputStream, passwords.txt);2.File方式
File myFile = new File(/path/to/file.png);
RequestParams params = new RequestParams();
try {
params.put(profile_picture, myFile);
} catch(FileNotFoundException e) {}
3.byte數組形式byte[] myByteArray = blah; RequestParams params = new RequestParams(); params.put(soundtrack, new ByteArrayInputStream(myByteArray), she-wolf.mp3);
AsyncHttpClient client = new AsyncHttpClient();
client.get(http://example.com/file.png, new FileAsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, File response) {
// Do something with the file `response`
}
});
AsyncHttpClient client = new AsyncHttpClient(); client.setBasicAuth(username,password/token); client.get(http://example.com);
AsyncHttpClient client = new AsyncHttpClient(); client.setBasicAuth(username,password, new AuthScope(example.com, 80, AuthScope.ANY_REALM)); client.get(http://example.com);
Android對話框裡面的輸入值獲取不到,空指針異常
寫的一個Android對話框,點擊按鈕獲取EditText裡面的值,這裡一直報空指針異常,研究了很長時間終於解決了。 異常如下: 我原來的代碼://更新對
Android一組WebView的隨機,順序,倒序加載
寫了個應用,實現了一組WebView的順序,倒序,和隨機加載。之中使用了延時,為什麼要使用呢?請看下圖: package com.zms.csdngo; im
[android] 天氣app布局練習
[android] 天氣app布局練習主要練習一下RelativeLayout和LinearLayout
Android如何通過手機獲取驗證碼來完成注冊功能
注冊很多app或者網絡賬戶的時候,經常需要手機獲取驗證碼,來完成注冊,那時年少,只是覺得手機獲取驗證碼這件事兒很好玩,並沒有關心太多,她是如何實現的,以及她背後的故事到底