Search code examples
androidxmljsonparsingjsonexception

Why Is JSON request returning as XML?


I'm a noob to android and I'm trying to parse a JSON from this link: "http://services.packetizer.com/spotprices/?f=json". However, when I send my request to parse it, i receive an error saying..." Error parsing data org.json.JSONException: Value xml of type java.lang.String cannot be converted to JSONObject". This is baffling to say the least because the link is obviously a JSON. Any help solving this is greatly appreciated.

My Code:

JSONObject json = JSONfunctions.getJSONfromURL("http://services.packetizer.com/spotprices/?f=json");                    
                        if(json==null){
                            //Do Nothing
                        }else{
                            String usdgold = json.getString("gold");             
                            livespotgold = Double.parseDouble(usdgold);
                            storedspotgold=livespotgold;
                            Log.e("Spot Gold Packetizer", String.valueOf(livespotgold));

                            String usdsilver = json.getString("silver");             
                            livespotsilver = Double.parseDouble(usdsilver);
                            storedspotsilver=livespotsilver;
                            Log.e("Spot Silver Packetizer", String.valueOf(livespotsilver));
                            haveSpot = true;
                        }

Solution

  • I assume you're using the JSONfunctions class from here or a modified version of it (as you're receiving a JSONObject and not a JSONArray).

    Note that that code sends an HTTP POST. This endpoint is returning XML when you send it a POST. You need to change the code to send an HTTP GET:

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpget);