gpt4 book ai didi

java - 我的 Android 应用程序中出现 IOException

转载 作者:行者123 更新时间:2023-12-01 11:08:33 25 4
gpt4 key购买 nike

我在 Android 应用程序中收到 IOException,但我找不到发生此异常的原因。我正在尝试连接到博客,稍后将解析 JSON。StackTrace 显示它无法解析 URL,但是当我在浏览器中打开 URL 时,它工作正常。我收到 java.net.unknownhostexception 无法解析主机 错误

这是代码:

package blogreader.com.example.android.blogreader;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainListActivity extends ListActivity {
protected String[] mAndroidNames;
public static final int NUMBER_OF_POSTS=20;
public static final String TAG=MainListActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_list);
GetBlogPostClass getBlogPostClass=new GetBlogPostClass();
getBlogPostClass.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_list, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
private class GetBlogPostClass extends AsyncTask<Object,Void,String>{

@Override
protected String doInBackground(Object... arg0) {
int responseCode=-1;
try {
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
HttpURLConnection connection =(HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode =connection.getResponseCode();
Log.i(TAG, "Code" + responseCode);

}
catch (MalformedURLException e)
{
Log.e(TAG,"MalformedException Caught",e);
}
catch (IOException e)
{
Log.e(TAG,"IOException Caught",e);
}
catch (Exception e)
{
Log.e(TAG,"Exception Caught",e);
}

return "Code:"+responseCode;
}
}
}

这是 Logcat:

09-18 12:52:50.609    1848-1848/blogreader.com.example.android.blogreader I/art﹕ Not late-enabling -Xcheck:jni (already on)
09-18 12:52:51.335 1848-1855/blogreader.com.example.android.blogreader W/art﹕ Suspending all threads took: 161.519ms
09-18 12:52:51.367 1848-1872/blogreader.com.example.android.blogreader D/OpenGLRenderer﹕ Render dirty regions requested: true
09-18 12:52:51.368 1848-1848/blogreader.com.example.android.blogreader D/﹕ HostConnection::get() New Host Connection established 0xae1d4d40, tid 1848
09-18 12:52:51.381 1848-1848/blogreader.com.example.android.blogreader D/Atlas﹕ Validating map...
09-18 12:52:51.481 1848-1872/blogreader.com.example.android.blogreader D/﹕ HostConnection::get() New Host Connection established 0xa6c3f080, tid 1872
09-18 12:52:51.762 1848-1872/blogreader.com.example.android.blogreader I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-18 12:52:51.785 1848-1855/blogreader.com.example.android.blogreader W/art﹕ Suspending all threads took: 178.024ms
09-18 12:52:51.805 1848-1872/blogreader.com.example.android.blogreader D/OpenGLRenderer﹕ Enabling debug mode 0
09-18 12:52:51.851 1848-1872/blogreader.com.example.android.blogreader W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-18 12:52:51.851 1848-1872/blogreader.com.example.android.blogreader W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c420a0, error=EGL_SUCCESS
09-18 12:52:51.930 1848-1871/blogreader.com.example.android.blogreader E/MainListActivity﹕ IOException Caught
09-18 12:52:53.747 1848-1855/blogreader.com.example.android.blogreader W/art﹕ Suspending all threads took: 27.251ms
09-18 12:53:25.143 1848-1872/blogreader.com.example.android.blogreader W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-18 12:53:25.143 1848-1872/blogreader.com.example.android.blogreader W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c420a0, error=EGL_SUCCESS
09-18 12:53:25.902 1848-2045/blogreader.com.example.android.blogreader E/MainListActivity﹕ IOException Caught

堆栈跟踪:

09-18 14:04:48.483    1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "blog.teamtreehouse.com": No address associated with hostname
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:457)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at blogreader.com.example.android.blogreader.MainListActivity$GetBlogPostClass.doInBackground(MainListActivity.java:60)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at blogreader.com.example.android.blogreader.MainListActivity$GetBlogPostClass.doInBackground(MainListActivity.java:51)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-18 14:04:48.484 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
09-18 14:04:48.485 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
09-18 14:04:48.485 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at libcore.io.Posix.android_getaddrinfo(Native Method)
09-18 14:04:48.485 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
09-18 14:04:48.485 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
09-18 14:04:48.485 1852-2058/blogreader.com.example.android.blogreader W/System.err﹕ ... 17 more
09-18 14:04:51.815 1852-1859/blogreader.com.example.android.blogreader W/art﹕ Suspending all threads took: 21.107ms
09-18 14:06:29.117 1852-1859/blogreader.com.example.android.blogreader W/art﹕ Suspending all threads took: 11.632ms
09-18 14:12:30.127 1852-1859/blogreader.com.example.android.blogreader W/art﹕ Suspending all threads took: 11.489ms

最佳答案

可能是以下内容的重复 - Unable to resolve host "<insert URL here>" No address associated with hostname

如果您已授予必要的权限,请检查您的 list 。

关于java - 我的 Android 应用程序中出现 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com