Pregunta

I am trying to parse json content from a website and its downloading some html file instead of json objects. Here is the code and error shown.(FYI I am parsing json and displaying that in the log cat)

This is MainActivity.java

   public class MainActivity extends Activity {

    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new MyAsyncTask().execute();
        lv = (ListView) findViewById(R.id.listView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class MyAsyncTask extends AsyncTask<String, String, String> {

        String url = "http://walmartlabs.api.mashery.com/v1/taxonomy?format=json&apiKey=yhg57ygb3mdk8ywuwdsvgews";
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Please wait...");
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            JSONParser parser = new JSONParser();
            JSONObject array = parser.getJSON(url);
     return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }

  }

This is Parser class: JSONParser.java

public class JSONParser {
    JSONObject jarray;
    String line;

    JSONObject getJSON(String url) {
        final StringBuilder sb = new StringBuilder();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        /* HttpGet httpPost = new HttpGet(url); */

        try {
            HttpResponse httpRes = httpClient.execute(httpPost);
            HttpEntity entity = httpRes.getEntity();
            InputStream stream = entity.getContent();
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(stream));

            while ((line = reader.readLine()) != null) {
                sb.append(line);
                Log.i("PARSED", line);
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            jarray = new JSONObject(sb.toString());
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jarray;
    }

}

The image shows the extracted data from json and its error Error shown in the logcat

Continuation to the other image

¿Fue útil?

Solución

When trying a POST on that url, I get the same error as you, but a GET returns:

{"categories":[ {
  "id" : "5438",
  "name" : "Apparel",
  "path" : "Apparel",
  "children" : [ {
    "id" : "5438_426265",
    "name" : "Accessories",
    "path" : "Apparel/Accessories",
    "children" : [ {
      "id" : "5438_426265_1043621",
      "name" : "Bandanas",
      "path" : "Apparel/Accessories/Bandanas"
    }, {
...

So what you need to do is change the HttpPost into a HttpGet.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top