Android 连接到connect()上的url时发生IO异常
在运行应用程序检查json字符串时,我有以下异步任务。当到达“urlconnection.connect()”时,会出现IO异常。Logcat只是显示此异常,没有任何解释。请帮助我哪里出了问题Android 连接到connect()上的url时发生IO异常,android,android-asynctask,httpurlconnection,Android,Android Asynctask,Httpurlconnection,在运行应用程序检查json字符串时,我有以下异步任务。当到达“urlconnection.connect()”时,会出现IO异常。Logcat只是显示此异常,没有任何解释。请帮助我哪里出了问题 public class FetchWeatherTask extends AsyncTask<Void,Void,Void> { private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
public class FetchWeatherTask extends AsyncTask<Void,Void,Void>
{
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7");
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
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) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
Log.v(LOG_TAG,"Forecast JSON string"+forecastJsonStr);
} catch (IOException e) {
Log.e("PlaceholderFragment", "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("PlaceholderFragment", "Error closing stream", e);
}
}
}
return null;
}
logcat中的第121行是“urlconnection.connect()”
编辑:
运行应用程序时,我遇到错误:
Network on main thread exception
strictmode android block guard policy on network exception
Network on main thread exception
strictmode android block guard policy on network exception
在UI线程上有网络密集型调用时会出现这些异常。可以找到答案。为了避免出现这种情况,我在主活动的oncreate方法中添加了以下代码:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
此外,建议使用异步任务进行网络密集型调用。这就解决了问题。谢谢大家的帮助 将INTERNET权限添加到清单文件中
<uses-permission android:name="android.permission.INTERNET" />
删除urlConnection.connect();行,我认为urlConnection=(HttpURLConnection)url.openConnection();已经足够了尝试这样做:
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
请尝试以下操作:
...
urlConnection.setDoInput(true);
...
urlConnection.connect();
InputStream inputStream = null;
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}
...
此外,使用e.printStackTrace()代码>而不是Log.e(“占位符片段”,“错误”,e)代码>用于完整日志CAT
更新:
代替
FetchWeatherTask f=新的FetchWeatherTask();
f、 doInBackground(空)
借
新建FetchWeatherTask().execute()
希望这有帮助 在运行应用程序时,我遇到了错误:
Network on main thread exception
strictmode android block guard policy on network exception
Network on main thread exception
strictmode android block guard policy on network exception
在UI线程上有网络密集型调用时会出现这些异常。可以找到答案。为了避免出现这种情况,我在主活动的oncreate方法中添加了以下代码:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
此外,建议使用异步任务执行网络密集型调用。
这就解决了问题。谢谢大家的帮助 它已经添加了:我试过了..但是调试控件一直运行到InputstreamReader,最后阻塞。它跳过了中间的语句,但是你有错误吗?如果不是,那么您处理/读取数据的方式可能有问题。当我删除connect()方法时,我得到了相同的异常。我认为罪魁祸首是urlconnection对象,因为在每种情况下,connect()和getinputstream()都是在urlconnection上调用的。您尝试过删除setRequestMethod(“get”)吗行?我已经在IDE中尝试了你的代码,它没有给出任何异常。顺便说一句,如果你没有添加,也可以添加相同的内容,但例外情况没有变化。我也尝试使用不同的URL,但出现了相同的异常。我认为“urlconnection”对象是罪魁祸首。如果我删除.connect()方法,它会一直到达inputstreamreader(其中有对“urlconnection”的函数调用),然后进入catch块。你发现“urlconnection”对象有什么奇怪的地方吗?你的代码很好,你使用的是仿真器还是真实的设备?让我们来看看。调试控件直到Inputstream声明才到达。在.connect()之后,它转到catch block..检查手机上的wifi、飞机模式。此外,如果logcat有更多内容,请发布它们