简体   繁体   English

运行 getHttpGet 方法时出现 Java 错误

[英]Java Error when running getHttpGet method

I'm trying the sample code and I get error from this我正在尝试示例代码,但出现错误

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

It says cannot resolve symbol 'getHttpGet(java.lang.String)' I'm already added httpclient-4.1.jar and httpcore-4.0.jar Is there any library file I have to add more ?它说无法解析符号 'getHttpGet(java.lang.String)' 我已经添加了 httpclient-4.1.jar 和 httpcore-4.0.jar 是否有任何库文件我必须添加更多? Thanks for help !感谢帮助 !

Use Loopj http://loopj.com/android-async-http/ it's the most straight forward http response/request library and with a light .jar file.使用 Loopj http://loopj.com/android-async-http/它是最直接的 http 响应/请求库和一个轻量级的 .jar 文件。 Here is a small example:这是一个小例子:

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
    }
});

Replace http://www.yourtargeturl.com with the url you want to hit and get the json response.http://www.yourtargeturl.com替换为您要点击的网址并获得 json 响应。

The onSuccess will give you the response of your GET request. onSuccess 将为您提供 GET 请求的响应。 You need to parse the byte[] response like this:您需要像这样解析 byte[] 响应:

String str = new String(response, "UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM