0

URL JSON Parsing Error. Please check my code.

{
    "info": "Central Bank of Myanmar",
    "description": "Official Website of Central Bank of Myanmar",
    "timestamp": "1448611200",
    "rates": 
{

    "USD": "1,300.0",
    "CZK": "51.055",
    "JPY": "1,060.2",
    }

}

Activity code is:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.json_object);

    TextView output = (TextView) findViewById(R.id.textView1);

    String data = "";
    try {
        String ka =callURL("http://forex.cbm.gov.mm/api/latest");

        JSONObject object = new JSONObject(ka);
        JSONObject servicedata = object.getJSONObject("rates");
        String USD = servicedata.getString("USD");

        data += "USD Currency " + USD +" ";
        output.setText(data);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Call URL give me error.

public static String callURL(String myURL) {
    System.out.println("Requeted URL:" + myURL);
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null)
            urlConn.setReadTimeout(60 * 1000);
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(),
                    Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }
        in.close();
    } catch (Exception e) {
        throw new RuntimeException("Exception while calling URL:"+ myURL, e);
    }

    return sb.toString();
}
4
  • Please, show your logcat. Commented Nov 28, 2015 at 19:19
  • It's not clear exactly what error you're receiving, and where. Commented Nov 28, 2015 at 19:21
  • And your json is not valid! Commented Nov 28, 2015 at 19:21
  • Maybe it is android.os.NetworkOnMainThreadException? Commented Nov 28, 2015 at 19:25

2 Answers 2

1

Try with android volly library It is developed by Google. You can easily convert json to java and vice versa.

Sign up to request clarification or add additional context in comments.

Comments

0

if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }

I put this code under the setContentView and code it work. Thank your advertises. I spent the time about one day for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.