Question

A week ago (Feb 2014) I downloaded the latest Android SDK with Eclipse. I’m new to it, but not dumb to Java programming, yet this problem is just killing me.

I was trying to do a simple tutorial on downloading a file from the Internet. After failing several tutorials I finally got to this one. But in the emulator this app fails connecting to Internet. I added simple TextView flags to track down the problem, and it’s always httpConn.connect();.

Here’s the kicker – when this app is installed onto my Samsung Galaxy Y (GT-S5360) it works OK.

In SDK Manager installed Google Web Driver – Emulator browser connects to the Internet. But no matter what I do, the emulator just doesn’t let my app to connect.

Yes, the uses-permission android:name="android.permission.INTERNET" /> exists.

Yes, the 3G bars are present.

Yes, the link to the file is spelled correctly.

No, manually installing the app every time onto an actual device is not a good solution for the work I’m doing.

The PC uses a cabled Internet connection. Tried the emulator on Windows7 64bit (with and without admin wrights), and the 32bit Android SDK emulator on WindowsXP 32bit. Restarted Eclipse. Downloaded the SDK again. Restarted the PC. Tried many suggestions, including "-dns-server 8.8.8.8,8.8.4.4". All fail to connect at httpConn.connect();.

The code is fine and simple. What is the freshly download new Android SDK emulator missing?!

Steps to replicate:

1) Download the latest Android SDK from the official site.

2) Run SDK manager and install default suggestions + Google Web Driver

3) Create the application from this tutorial: http://www.edumobile.org/android/android-beginner-tutorials/downloading-an-image-from-the-server-and-displaying-it-on-screen/

4) Run the application as Android Application – get white screen because httpConn.connect(); failed.

Here is the upgraded LogCat error report:

D/dalvikvm(933): Not late-enabling CheckJNI (already on)
W/System.err(933): java.io.IOException: Error connecting
W/System.err(933):  at    com.example.testfiledownlaod3.downloadImage.OpenHttpConnection(downloadImage.java:70)
W/System.err(933):  at    com.example.testfiledownlaod3.downloadImage.DownloadImage(downloadImage.java:83)
W/System.err(933):  at com.example.testfiledownlaod3.downloadImage.onCreate(downloadImage.java:24)
W/System.err(933):  at android.app.Activity.performCreate(Activity.java:5231)
W/System.err(933):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
W/System.err(933):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
W/System.err(933):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
W/System.err(933):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
W/System.err(933):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
W/System.err(933):  at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err(933):  at android.os.Looper.loop(Looper.java:136)
W/System.err(933):  at android.app.ActivityThread.main(ActivityThread.java:5017)
W/System.err(933):  at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(933):  at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err(933):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
W/System.err(933):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
W/System.err(933):  at dalvik.system.NativeStart.main(Native Method)
W/System.err(933): Caused by: android.os.NetworkOnMainThreadException
W/System.err(933):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
W/System.err(933):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
W/System.err(933):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W/System.err(933):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err(933):  at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
W/System.err(933):  at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
W/System.err(933):  at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
W/System.err(933):  at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
W/System.err(933):  at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
W/System.err(933):  at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
W/System.err(933):  at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
W/System.err(933):  at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
W/System.err(933):  at com.example.testfiledownlaod3.downloadImage.OpenHttpConnection(downloadImage.java:54)
W/System.err(933):  ... 16 more
D/gralloc_goldfish(933): Emulator without GPU emulation detected.
Was it helpful?

Solution

Thank you for giving me straws to grasp to. After getting Caused by: android.os.NetworkOnMainThreadException I tracked the problem down to AsyncTask implementation. Turns out the emulator doesn't like it when you use all your Internet processes in one class. So after browsing through other examples I eventually broke the tutorial down into two classes.

package com.example.testfiledownlaod3;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class downloadImage extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView img = (ImageView) findViewById(R.id.imageView1);
        String URL = "http://www.allindiaflorist.com/imgs/arrangemen4.jpg";
        img.setTag(URL);
        new DownloadImageTask().execute(img);
    }
}

and

package com.example.testfiledownlaod3;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

public class DownloadImageTask extends AsyncTask<ImageView, Void, Bitmap>
{
    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews)
    {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

    private Bitmap download_Image(String url)
    {
        Bitmap bmp=null;
        try
        {
            URL ulrn = new URL(url);
            HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
            InputStream instr = con.getInputStream();
            bmp = BitmapFactory.decodeStream(instr);
            instr.close();
        }
        catch (Exception ex){}
        return bmp;
    }

    @Override
    protected void onPostExecute(Bitmap result)
    {
        imageView.setImageBitmap(result);
    }
}

Works on the emulator and on my Samsung Galaxy Y (GT-S5360)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top