I have been getting IOException in my Android App Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of I have been getting IOException in my Android App without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have been getting IOException in my Android App,but i cannot find why this exception has occured.I am trying to make connection to a blog,and will later parse the JSON.StackTrace shows it is unable to resolve URL,but when i open the URL in browser it is working perfectly.I am getting java.net.unknownhostexception unable to resolve host error

Here is the code:

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;
        }
    }
}

Here is the 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

StackTrace:

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

Answer

Perhaps a duplicate of the following – Unable to resolve host “<insert URL here>” No address associated with hostname

Please check your manifest if you have given the necessary permissions.

We are here to answer your question about I have been getting IOException in my Android App - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji