【问题标题】:I am trying to parse a json and its downloading some html instead of json content我正在尝试解析 json 并下载一些 html 而不是 json 内容
【发布时间】:2014-03-19 17:50:54
【问题描述】:

我正在尝试从网站解析 json 内容并下载一些 html 文件而不是 json 对象。这是显示的代码和错误。(仅供参考,我正在解析 json 并将其显示在 log cat 中)

这是 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();
            }
        }
    }

  }

这是解析器类: 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;
    }

}

图像显示了从 json 中提取的数据及其错误

【问题讨论】:

    标签: android json android-json


    【解决方案1】:

    在该网址上尝试POST 时,我得到与您相同的错误,但GET 返回:

    {"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"
        }, {
    ...
    

    所以你需要做的是将HttpPost 更改为HttpGet

    【讨论】:

    • 但是 HttpPost 在这种情况下不工作的原因是什么?
    • @srujanmaddula,服务器根本不允许 POST 请求:只允许 GET 请求。 POST(或 PUT)通常与 [在服务器上] 更改资源相关联。 GET 通常与检索信息相关联(这是您尝试做的:检索类别列表)。也看看这个问答:stackoverflow.com/questions/18395523/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    相关资源
    最近更新 更多