在 connect() 处连接到 url 时出现 IO 异常
IO Exception while connecting to the url at the connect()
虽然 运行 一个应用程序检查 json 字符串,但我有以下 Async Task.It 在到达 "urlconnection.connect()" 时给出 IO 异常。 Logcat 只显示此异常,没有任何更多 explanation.Please 帮助我哪里出错了。
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如下:
08-18 20:12:14.608: E/PlaceholderFragment(15407): Error
08-18 20:12:14.608: E/PlaceholderFragment(15407): java.io.IOException
08-18 20:12:14.608: E/PlaceholderFragment(15407): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:87)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.example.sunshineapp.ForecastFragment$FetchWeatherTask.doInBackground(Forecas tFragment.java:121)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.example.sunshineapp.ForecastFragment.onOptionsItemSelected(ForecastFragment. java:55)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.Fragment.performOptionsItemSelected(Fragment.java:1801)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java :1959)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.Activity.onMenuItemSelected(Activity.java:2569)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.app.ActionBarActivityDelegate.onMenuItemSelected(ActionBarActivityDelegate.java:74)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:556)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:169)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2815)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AbsListView.run(AbsListView.java:3574)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.os.Handler.handleCallback(Handler.java:800)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.os.Handler.dispatchMessage(Handler.java:100)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.os.Looper.loop(Looper.java:194)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.ActivityThread.main(ActivityThread.java:5371)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at java.lang.reflect.Method.invoke(Method.java:525)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at dalvik.system.NativeStart.main(Native Method)
logcat 中的第 121 行是 "urlconnection.connect()"。
编辑:
虽然 运行 我遇到了错误的应用程序:
Network on main thread exception
strictmode android block guard policy on network exception
当UI上有网络密集调用时会出现这些异常 thread.The答案可以找到here。为了避免我在oncreate中添加了以下代码主要方法 activity:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
此外,建议使用 Async Task 进行网络密集型调用。
这解决了这个问题。感谢大家的帮助!!
将 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("PlaceholderFragment", "Error ", e);
以获得完整的 logcat
更新:
代替
FetchWeatherTask f = new FetchWeatherTask();
f.doInBackground(空);
来自
新的 FetchWeatherTask().execute();
希望对您有所帮助!
虽然 运行 我遇到了错误的应用程序:
Network on main thread exception
strictmode android block guard policy on network exception
当UI上有网络密集调用时会出现这些异常 thread.The答案可以找到here。为了避免我在oncreate中添加了以下代码主要方法 activity:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
此外,建议使用异步任务进行网络密集型调用。
这解决了这个问题。感谢大家的帮助!!
虽然 运行 一个应用程序检查 json 字符串,但我有以下 Async Task.It 在到达 "urlconnection.connect()" 时给出 IO 异常。 Logcat 只显示此异常,没有任何更多 explanation.Please 帮助我哪里出错了。
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如下:
08-18 20:12:14.608: E/PlaceholderFragment(15407): Error
08-18 20:12:14.608: E/PlaceholderFragment(15407): java.io.IOException
08-18 20:12:14.608: E/PlaceholderFragment(15407): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:87)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.example.sunshineapp.ForecastFragment$FetchWeatherTask.doInBackground(Forecas tFragment.java:121)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.example.sunshineapp.ForecastFragment.onOptionsItemSelected(ForecastFragment. java:55)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.Fragment.performOptionsItemSelected(Fragment.java:1801)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java :1959)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.Activity.onMenuItemSelected(Activity.java:2569)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.app.ActionBarActivityDelegate.onMenuItemSelected(ActionBarActivityDelegate.java:74)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:556)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.support.v7.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:169)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2815)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.widget.AbsListView.run(AbsListView.java:3574)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.os.Handler.handleCallback(Handler.java:800)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.os.Handler.dispatchMessage(Handler.java:100)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.os.Looper.loop(Looper.java:194)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at android.app.ActivityThread.main(ActivityThread.java:5371)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at java.lang.reflect.Method.invoke(Method.java:525)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-18 20:12:14.608: E/PlaceholderFragment(15407): at dalvik.system.NativeStart.main(Native Method)
logcat 中的第 121 行是 "urlconnection.connect()"。
编辑:
虽然 运行 我遇到了错误的应用程序:
Network on main thread exception
strictmode android block guard policy on network exception
当UI上有网络密集调用时会出现这些异常 thread.The答案可以找到here。为了避免我在oncreate中添加了以下代码主要方法 activity:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
此外,建议使用 Async Task 进行网络密集型调用。
这解决了这个问题。感谢大家的帮助!!
将 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("PlaceholderFragment", "Error ", e);
以获得完整的 logcat
更新: 代替 FetchWeatherTask f = new FetchWeatherTask(); f.doInBackground(空);
来自
新的 FetchWeatherTask().execute();
希望对您有所帮助!
虽然 运行 我遇到了错误的应用程序:
Network on main thread exception
strictmode android block guard policy on network exception
当UI上有网络密集调用时会出现这些异常 thread.The答案可以找到here。为了避免我在oncreate中添加了以下代码主要方法 activity:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
此外,建议使用异步任务进行网络密集型调用。 这解决了这个问题。感谢大家的帮助!!