使用Loopj http://loopj.com/android-async-http/這是最直接的http響應/請求庫和一個輕的.jar文件。這裏是一個小例子:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.yourtargeturl.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
}
});
替換http://www.yourtargeturl.com你想打,並獲得JSON響應的URL。
onSuccess會爲您提供GET請求的響應。你需要分析這樣的字節[]迴應:
String str = new String(response, "UTF-8");
您需要定義與以下簽名'字符串getHttpGet(字符串URL)'的方法。 – Tunaki