Java 正在获取IOException,但没有错误消息
使用Android Studio 2.0 没有得到任何输出Java 正在获取IOException,但没有错误消息,java,android,android-studio,httpurlconnection,openweathermap,Java,Android,Android Studio,Httpurlconnection,Openweathermap,使用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.getRequest
Log.v(LOG_TAG, "Response Code" + urlConnection.getResponseCode());
Log.v(LOG_TAG, "Error Stream" + urlConnection.getErrorStream());
Log.v(LOG_TAG, "Request Method" + urlConnection.getRequestMethod());
在浏览器上使用内置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/FetchWeatherTask:错误
不知道,但是,我将手机连接到wifi网络,然后再次发送请求,我得到了JSON响应
我确认了,没有网络连接,我没有得到输出
关于
04-22 18:42:16.444 9011-9011/com.android.serverwarrior.sunshine
E/ViewRootImpl:sendUserActionEvent()mView==null
这是因为三星设备,我从其他帖子中得到的关于这个错误的信息。所以这一部分可以忽略。你不接受这一行的异常吗?Log.e(Log_标签,“Error”,e);如果已正确设置过滤器,则错误消息始终显示在logcat中。肯定还有更多…@Opiatefuchs我没有在logcat上使用任何过滤器。@RobertoDeLaParra我已经发布了,我在logcat上得到了什么。@RobertoDeLaParra如果你想让我在那里使用其他东西,请让我知道。是的……三星设备……我在三星galaxy s3上也遇到了一些问题……它有自己的生活,试图让你烦恼……@Opiatefuchs知道这些问题不是新的,但仍然没有解决,这很糟糕。@Opiatefuchs尝试以管理员的身份运行Android Studio,它确实比平时快,这是我在这期间注意到的,因为我花了将近6个小时。