【问题标题】:Android: AsyncHttpClient reponse goes to OnFailureAndroid:AsyncHttpClient 响应转到 OnFailure
【发布时间】:2016-04-23 12:25:41
【问题描述】:

我正在尝试从 URL 解析数据。我在移动浏览器中测试的 URL 工作正常。我正在尝试通过 AsyncHttpClient 解析数据。执行后将其转到 onFailure 方法。

在依赖项中添加

   compile 'com.loopj.android:android-async-http:1.4.9'

我导入

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import cz.msebera.android.httpclient.Header;

..

AsyncHttpClient client = new AsyncHttpClient();
        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                try {
                    pg1.dismiss();
                    String jsonStr = new String(responseBody, "UTF-8");
                    Log.e("Tag ","jsonStr "+jsonStr);

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                pg1.dismiss();
                Log.e("Tag ","fetch feeds fail");                 
            }
        });

网址在 GET 方法中 谁能帮我解决这个问题

【问题讨论】:

  • Log.e("Tag ","fetch feeds fail"); onFailure的所有参数
  • 网址是完整的,我正在尝试解析,我的问题是它不会去 onSuccess 而不是调用 onFailure。我登录 onFailure @ Selvin

标签: android json parsing android-asynctask


【解决方案1】:

我正在使用内部类扩展 AsncyTask 做同样的事情。

private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {

    @Override
    protected Weather doInBackground(String... params) {

        Weather weather = new Weather();
        data = ((new WeatherHttpClient()).getWeatherData(params[0], params[1]));

        Log.d("DATA", data);

        try {
            weathers = JSONWeatherParser.getWeather(data);

            // Let's retrieve the icon
            weather.iconData = ((new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return weather;

    }

在 WeatherHttpClient 类中,我使用带有 HttpUrlConnection 的 URL 获取数据。

【讨论】:

    【解决方案2】:

    只需在 onFailure() 方法中打印 responseBody。如果是超时异常;然后你可以设置 client.setTimeout(30000);在 client.get() 之前。

    检查您的网址;如果它在浏览器中给出响应,并检查键和值。可能是服务器没有给你任何响应。

    【讨论】:

    • onFailure 上的响应体 @Sachin salunkhe 将变为 null
    • 检查你的网址;如果它在浏览器中给出响应,并检查键和值。可能是服务器没有给你任何响应。
    • 我用浏览器和邮递员插件检查我得到了结果,也在 doinBackground() , onPostExecue 方法中得到了结果。但是在 AsyncHttpClient 方法中没有得到结果@Sachin salunkhe
    • 那么你必须实现所有版本的 onFailure() 方法,这样你才能得到准确的错误或 Eception
    猜你喜欢
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多