Android: read JSON data

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 as follows:

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

    
asked by anonymous 22.04.2016 / 23:12

2 answers

5

You have to pass the response 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 (it has to be used on 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");
    
23.04.2016 / 02:51
2

You are trying to parse from the site. However, you should do parse of the response (data) that this URL gives you. For example "

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

Note that the JSONObject object does not retrieve data from the site. You will have to use some library for this, such as OkHttp

    
23.04.2016 / 01:48