Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing JSON in Android?
    primarykey
    data
    text
    <p>Hello Android programmers,</p> <p>I am making a currency application and to do this, I am getting my currency rates from the following website (that returns json):</p> <p><a href="https://raw.github.com/currencybot/open-exchange-rates/master/latest.json" rel="nofollow">https://raw.github.com/currencybot/open-exchange-rates/master/latest.json</a></p> <p>Here is the method in my program that gets all of the rates found on the website.</p> <pre><code>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 &lt; 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; } </code></pre> <p>This method does not work for some reason. I will now post the LogCat in order to help me debug this method.</p> <pre><code>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.&lt;init&gt;(HttpConnection.java:71) 03-29 13:42:21.323: E/AndroidRuntime(678): at libcore.net.http.HttpConnection.&lt;init&gt;(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 </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload