0

I'm trying to read from a url the following data:

{"to": "DKK", "rate": 7.4417, "from": "EUR"}

And I'm trying to read it with this code:

JSONObject obj = new JSONObject();
JSONObject obj2 = obj.getJSONObject(site);
String to = (String)obj2.get("to");
Double rate = obj2.getDouble("rate");

And I get the following error:

org.json.JSONException: No value for http://rate-exchange-1.appspot.com/currency?from=EUR&to=DKK
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:609)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at com.converter.android.converter.ConvertActivity$Parse.doInBackground(ConvertActivity.java:1579)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at com.converter.android.converter.ConvertActivity$Parse.doInBackground(ConvertActivity.java:1547)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-22 23:06:56.114 19331-19991/com.converter.android.converter W/System.err:     at java.lang.Thread.run(Thread.java:818)

Can you please help me?

porthfind
  • 1,581
  • 3
  • 17
  • 30

1 Answers1

0

This is your problem, you can´t directly get a Json Object using the url, because isn´t a json object:

JSONObject obj2 = obj.getJSONObject(site);

You will check some examples here:

Android - download JSON file from url

Download the file and then you will use the getJSONObject() method.

Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268