JSON数据被视为XML,如何解决?

时间:2015-01-05 21:46:50

标签: android xml json

我试图通过此链接显示JSON数据。

http://opendata.rijksoverheid.nl/v1/sources/rijksoverheid/infotypes/schoolholidays/schoolyear/2015-2016?output=json

我不断得到的错误是:java.lang.string类型的值xml无法转换为JSONObject。但我很确定该链接显示JSON。有什么建议吗?

这是我到目前为止所做的:

public class MainActivity extends Activity {
    TextView txt1;
    TextView txt2;
    //TextView txt3;

    private static String url = "http://opendata.rijksoverheid.nl/v1/sources/rijksoverheid/infotypes/schoolholidays/schoolyear/2015-2016?output=json";
    private static final String TAG_CONTENT = "content";
    private static final String TAG_TITLE = "title";
    private static final String TAG_SCHOOLYEAR = "schoolyear";
    //private static final String TAG_EMAIL = "email";
    JSONArray content = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new JSONParse().execute();
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {

        protected void onPreExecute() {
            super.onPreExecute();
            txt1 = (TextView)findViewById(R.id.textView1);
            txt2 = (TextView)findViewById(R.id.textView2);
            //txt3 = (TextView)findViewById(R.id.textView3);
        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }

        protected void onPostExecute(JSONObject json) {
            try {
                content = json.getJSONArray(TAG_CONTENT);
                JSONObject c = content.getJSONObject(0);

                String title = c.getString(TAG_TITLE);
                String schoolyear = c.getString(TAG_SCHOOLYEAR);
                //String email = c.getString(TAG_EMAIL);

                txt1.setText(title);
                txt2.setText(schoolyear);
                //txt3.setText(email);
               } catch(JSONException e) {
                e.printStackTrace();
               }
        }
    }
}

public class JSONParser {
    static InputStream is = null;
    static JSONObject jobj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {
        // HTTP request
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch(ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch(Exception e) {
            Log.e("Error", "Error converting result" + e.toString());
        }
        // parse string to JSON object
        try {
            jobj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data" + e.toString());
        }
        // return JSON string
        return jobj;
    }
}

2 个答案:

答案 0 :(得分:0)

就加载JSON文件而言,您可以使用异步任务并下载JSON文件。

由于您提到了术语largescale,我假设您在阅读JSON文件时正在寻找一些快速的性能。

您可以尝试使用一些JSON解析库,而不是使用本机包(org.json)。

  1. GSON https://code.google.com/p/google-gson/
  2. 杰克逊http://wiki.fasterxml.com/JacksonDownload
  3. 希望这有帮助。

答案 1 :(得分:0)

在这一行中显然将HttpPost更改为HttpGet就可以了。但不知道为什么会这样。

HttpPost httpPost = new HttpPost(url);

to

HttpGet httpPost = new HttpGet(url);