2015-09-07 51 views
-1

當我試圖樣本代碼,我從這個Java錯誤運行getHttpGet方法

String url = "localhost/newfile.php"; 
     String resultServer = getHttpGet(url); 

它說無法解析符號「getHttpGet(java.lang.String中)」 我已經加入獲得錯誤httpclient-4.1.jar和httpcore-4.0.jar 是否有任何庫文件需要添加?感謝幫助 !

+0

您需要定義與以下簽名'字符串getHttpGet(字符串URL)'的方法。 – Tunaki

回答

1

使用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");