0

Hello Android programmers,

I am making a currency application and to do this, I am getting my currency rates from the following website (that returns json):

https://raw.github.com/currencybot/open-exchange-rates/master/latest.json

Here is the method in my program that gets all of the rates found on the website.

public String[] fetchCurrencyRates()
{
    String[] currencyValues = new String[currency_array.length]; //currency_array has the names of all the currencies
    try
    {
        URL currency = new URL("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");
        URLConnection c = currency.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String line;
        while ((line = in.readLine()) != null)
        {
            JSONArray ja = new JSONArray(line);
            for (int i = 0; i < ja.length(); i++)
            {
                JSONObject jo = (JSONObject) ja.get(i);
                currencyValues[i] = (jo.getString("rates"));
            }
        }
    }
    catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (JSONException e) {
    e.printStackTrace();
    }
    return currencyValues;
}

This method does not work for some reason. I will now post the LogCat in order to help me debug this method.

03-29 13:42:21.323: E/AndroidRuntime(678): FATAL EXCEPTION: main
03-29 13:42:21.323: E/AndroidRuntime(678): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sapra.currency/com.sapra.currency.TheUltimateCurrencyConverterActivity}: android.os.NetworkOnMainThreadException
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.os.Looper.loop(Looper.java:137)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.ActivityThread.main(ActivityThread.java:4424)
03-29 13:42:21.323: E/AndroidRuntime(678):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 13:42:21.323: E/AndroidRuntime(678):  at java.lang.reflect.Method.invoke(Method.java:511)
03-29 13:42:21.323: E/AndroidRuntime(678):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-29 13:42:21.323: E/AndroidRuntime(678):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-29 13:42:21.323: E/AndroidRuntime(678):  at dalvik.system.NativeStart.main(Native Method)
03-29 13:42:21.323: E/AndroidRuntime(678): Caused by: android.os.NetworkOnMainThreadException
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
03-29 13:42:21.323: E/AndroidRuntime(678):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
03-29 13:42:21.323: E/AndroidRuntime(678):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
03-29 13:42:21.323: E/AndroidRuntime(678):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-29 13:42:21.323: E/AndroidRuntime(678):  at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
03-29 13:42:21.323: E/AndroidRuntime(678):  at com.sapra.currency.TheUltimateCurrencyConverterActivity.fetchCurrencyRates(TheUltimateCurrencyConverterActivity.java:60)
03-29 13:42:21.323: E/AndroidRuntime(678):  at com.sapra.currency.TheUltimateCurrencyConverterActivity.onCreate(TheUltimateCurrencyConverterActivity.java:51)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.Activity.performCreate(Activity.java:4465)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-29 13:42:21.323: E/AndroidRuntime(678):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-29 13:42:21.323: E/AndroidRuntime(678):  ... 11 more

I have confirmed that this method is the cause of the problem, as I commented out the call to this method and the program did function as expected.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
sameetandpotatoes
  • 1,220
  • 3
  • 17
  • 35

3 Answers3

2

You are trying to fetch your JSON data via the web from the main thread. Starting in API lvl 10 or 11 I think this will raise an exception. Consider it the people who make the platform's way of telling you that it is a very bad idea to do network operations on the main thread. Move your call into a AsyncTask or use the Thread / Handler pattern to accomplish your work in the background.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
1

Caused by: android.os.NetworkOnMainThreadException

In Android Honeycomb and higher, network operations are not permitted on the main UI thread. Move them to an AsyncTask or a Service.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
1

You cannot perform network IO on the UI thread on Honeycomb. See this post: android.os.NetworkOnMainThreadException

Community
  • 1
  • 1
Chemik
  • 1,459
  • 13
  • 23