Android: read JSON data

Asked

Viewed 4,969 times

7

I have a url that gives me the data in this format:

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

I’m trying to read them this way:

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

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.current.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.current.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 someone help me?

2 answers

5


You have to pass the answer from the site to json, not the url. To get the answer use this class:

public class HttpConnections {
 //método get
public static String get(String urlString){
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String resposta = null;
    try {
        URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());

        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        StringBuffer buffer = new StringBuffer();
        while ((line = reader.readLine()) != null){
            buffer.append(line);
        }
        resposta = buffer.toString();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        if (urlConnection != null){
            urlConnection.disconnect();
        }
        try {
            reader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    return resposta;
}
}

Then just call passing your url(has to be used in another Thread).

String resposta = HttpConnections.get(" http://rate-exchange-1.appspot.com/currency?from=EUR&to=DKK");
JSONObject obj = new JSONObject(resposta);
String to = obj.getString("to");
Double rate = obj.getDouble("rate");
String from = obj.getString("from");
  • Thank you! It worked!

2

You’re trying to do parse website. However, you should do parse the answer (data) this URL gives you. For example"

JSONObject obj = new JSONObject(resposta);
String to = obj.getString("to");

Note that the Jsonobject object does not get the data from the site. You will have to use some library for this, for example Okhttp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.