1

使用过Android Studio 2.0获取IOException,使用没有错误信息的Android

没有得到输出使用内置URI浏览器的

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

给了我正确的结果。

之前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:Error

+0

是不是用这条线吞下异常? Log.e(LOG_TAG,“Error”,e);如果您设置了正确的过滤器,则 – raven

+0

错误消息始终在logcat中。必须有更多... – Opiatefuchs

+0

@Opiatefuchs我没有在logcat上使用任何过滤器。 –

回答

0

不知道,但我把手机连接到无线网络,并再次发送请求,我得到了JSON resposne。

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

关于

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

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

+0

是...三星设备....我也有一些问题与三星Galaxy S3 ...它有他自己的生活,并试图惹恼你..... – Opiatefuchs

+0

@Opiatefuchs这是非常糟糕的知道,问题并不新鲜但仍未解决。 –

+0

@Opiatefuchs尝试以管理员身份运行Android Studio,速度比平时快,这是我在此期间注意到的事情,因为我花了将近6小时。 –

相关问题