首页 文章

Android Volley没有来自StringRequest的回复

提问于
浏览
0

(对不起,任何英语错误,不是我的母语)

我正在尝试使用Volley和Jsoup解析html . 在调试/运行应用程序时,StringRequest dosent调用onResponse()或onErrorResponse(),基本上没有来自Response.Listener的响应或错误(我猜) . 所以我不能“开始”解析html因为永远不会调用onResponse() . 我寻找答案,似乎没有什么可以解决它 .

可能是RequestQueue缓存启动并且它没有尝试获得响应吗? (只是头脑风暴尝试了很多东西) .

  • 我只是试图从html中检索数据,所以我可以在我的应用程序中显示来自url的更新数据,是Volly方式还是我应该使用更简单的mathods?

这是我的排球StringRequest和RequestQueue:

String url = "http://www.fringeb7.co.il/";
final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    response_utf8 = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"),"UTF-8");
                    doc = Jsoup.parse(response_utf8);
                    Log.d("logr=",response);
                }
            },
        new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Log.d("log2=", error.toString());
                    requestQueue.stop();
                }
    });
    // Add the request to the RequestQueue.
    requestQueue.add(stringRequest);

2 回答

  • 0

    花了我很长时间,但没有问题 . 代码工作,而调试没有响应,但当我运行应用程序时,请求完成,我得到了想要的响应 .

    因此,如果有人有一个相似的问题,从调试的某些原因我dident获得响应,但在运行应用程序时其工作正常 . (简单的dident知道)

    首先尝试这个,然后像我一样在调试中浪费时间 .

  • 0

    如果您使用Jsoup,您可以以非常简单的方式获取它并进行解析:

    Document doc = Jsoup.connect("http://www.fringeb7.co.il/").get();
    

    如果您仍想使用Volley,则以下代码可在我的设备中完美运行:

    String url = "http://www.fringeb7.co.il/";
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            String response_utf8 = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"), "UTF-8");
                            Document doc = Jsoup.parse(response_utf8);
                            Log.d("logr=", "title = " + doc.title());
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        Log.d("log2=", error.toString());
                        //requestQueue.stop();
                    }
                });
        // Add the request to the RequestQueue.
        requestQueue.add(stringRequest);
        requestQueue.start();
    

    输出上面的代码:

    D/logr=: title = תיאטרון הפרינג' באר שבע
    

相关问题