【问题标题】:Getting IOException with no error message Android获取 IOException 且没有错误消息 Android
【发布时间】:2016-08-16 02:21:08
【问题描述】:

使用 Android Studio 2.0

没有输出

Log.v(LOG_TAG, "Response Code" + urlConnection.getResponseCode());
Log.v(LOG_TAG, "Error Stream" + urlConnection.getErrorStream());
Log.v(LOG_TAG, "Request Method" + urlConnection.getRequestMethod());    

在浏览器上使用 Built URI 可以得到正确的结果。

urlConnection.connect(); 调用 getRequestMethod() 之前给我方法名称 GET 。

代码

try {
    // Construct the URL for the OpenWeatherMap query
    // Possible parameters are avaiable at OWM's forecast API page, at
    // http://openweathermap.org/API#forecast
    final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
    final String QUERY_PARAM = "q";
    final String FORMAT_PARAM = "mode";
    final String UNITS_PARAM = "units";
    final String DAYS_PARAM = "cnt";
    final String APPID_PARAM = "APPID";

    Uri builtUri = Uri
            .parse(FORECAST_BASE_URL)
            .buildUpon()
            .appendQueryParameter(QUERY_PARAM, params[0])
            .appendQueryParameter(FORMAT_PARAM, format)
            .appendQueryParameter(UNITS_PARAM, units)
            .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
            .appendQueryParameter(APPID_PARAM,
                    BuildConfig.OPEN_WEATHER_MAP_API_KEY).build();

    URL url = new URL(builtUri.toString());

    Log.v(LOG_TAG, "Built URI " + builtUri.toString());

    // Create the request to OpenWeatherMap, and open the connection
    urlConnection = (HttpURLConnection) url.openConnection();
    // urlConnection.setRequestMethod("GET");
    urlConnection.connect();
    Log.v(LOG_TAG, "Response Code" + urlConnection.getResponseCode());
    Log.v(LOG_TAG, "Error Stream" + urlConnection.getErrorStream());
    Log.v(LOG_TAG, "Request Method" + urlConnection.getRequestMethod());

    // Read the input stream into a String
    // InputStream inputStream = urlConnection.getInputStream();
    InputStream inputStream;
    if (urlConnection.getResponseCode() >= 400) {
        inputStream = urlConnection.getErrorStream();
    } else {
        inputStream = urlConnection.getInputStream();
    }
    // Log.v(LOG_TAG, "Response Code" +urlConnection.getHeaderFields());

    StringBuffer buffer = new StringBuffer();
    if (inputStream == null) {
        // Nothing to do.
        return null;
    }
    reader = new BufferedReader(new InputStreamReader(inputStream));

    String line;
    while ((line = reader.readLine()) != null) {
        // Since it's JSON, adding a newline isn't necessary (it won't
        // affect parsing)
        // But it does make debugging a *lot* easier if you print out
        // the completed
        // buffer for debugging.
        buffer.append(line + "\n");
    }

    if (buffer.length() == 0) {
        // Stream was empty. No point in parsing.

        return null;
    }
    forecastJsonStr = buffer.toString();
    Log.v(LOG_TAG, "JSON Response " + forecastJsonStr);
} catch (IOException e) {
    Log.e(LOG_TAG, "Error", e);
    // If the code didn't successfully get the weather data, there's no
    // point in attemping
    // to parse it.
    return null;
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
    if (reader != null) {
        try {
            reader.close();
        } catch (final IOException e) {
            Log.e(LOG_TAG, "Error closing stream", e);
        }
    }
}

错误日志

04-22 18:42:16.444 9011-9011/com.android.serverwarrior.sunshine E/ViewRootImpl: sendUserActionEvent() mView == null

04-22 18:42:17.405 9011-9314/com.android.serverwarrior.sunshine E/FetchWeatherTask:错误

【问题讨论】:

  • 你没有吞下这条线的异常吗? Log.e(LOG_TAG, "错误", e);
  • 如果您已正确设置过滤器,则错误消息始终在 logcat 中。一定还有更多……
  • @Opiatefuchs 我没有在 logcat 上使用任何过滤器。
  • @RobertoDeLaParra 我已经发布了,我在 logcat 上得到了什么。
  • @RobertoDeLaParra 如果你想让我在那里使用其他东西,请告诉我。

标签: java android android-studio httpurlconnection openweathermap


【解决方案1】:

不知道,但是,我将手机连接到 wifi 网络,然后再次发送请求,我得到了 JSON 响应。

我确认了,没有网络连接,我没有得到输出。

关于

04-22 18:42:16.444 9011-9011/com.android.serverwarrior.sunshine E/ViewRootImpl: sendUserActionEvent() mView == null

这是因为三星设备,我从其他帖子中得到的这个错误。所以这部分可以忽略。

【讨论】:

  • 是的......三星设备......我也有三星galaxy s3的一些问题......它有自己的生活并试图惹恼你......
  • @Opiatefuchs 知道这些问题不是新问题但仍未解决,这很糟糕。
  • @Opiatefuchs 尝试以管理员身份运行 Android Studio,它确实比平时快,这是我在此过程中注意到的,因为我花了将近 6 个小时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 2021-07-19
  • 2019-07-29
相关资源
最近更新 更多