JSON解析器返回一些HTML / CSS内容而不是实际的JSON字符串

时间:2013-04-12 20:03:06

标签: android json

我有asyncTask喜欢这个:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;

public class OfficeJSONParser extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog progressDialog;
    InputStream inputStream = null;
    String result = "";
    Context c;
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    String url;

    public OfficeJSONParser(Context c, String url) {
        this.c = c;
        this.url = url;
    }

    protected void onPreExecute() {
        progressDialog = new ProgressDialog(c);
        progressDialog.setMessage("Downloading your data...");
        progressDialog.show();
        progressDialog.setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                OfficeJSONParser.this.cancel(true);
            }
        });
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        return getJSONFromUrl(url);
    }

    protected void onPostExecute(JSONObject jObj) {
        JSONObject json = jObj;
    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            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("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json.substring(json.indexOf("{"),
                    json.lastIndexOf("}") + 1));
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;

    }
}

我知道提供的URL是一个有效的JSON文档,但我的解析器会返回:

{"MARGIN-LEFT":"30px","PADDING-BOTTOM":"2em","FONT-SIZE":"0.7em"} 

为什么呢?怎么了?

编辑:

JSON是挪威语,但结构很容易理解:

http://data.helsenorge.no/External.svc/Services/KA02/10.75/59.91

2 个答案:

答案 0 :(得分:1)

我已经编译了你的代码并运行它,我不确定为什么另一个答案已被弃用,除了没有提供有关该主题的更多信息。我将你的HttpPost更改为HttpGet,我已经回复给我的JSON字符串确实是该站点的JSON内容。只需改变:

HttpPost httpPost = new HttpPost(url);

HttpGet httpGet = new HttpGet(url);

并记录或调试从

获得的JSON字符串
json = sb.toString();

你应该得到你想要的。您需要做的就是将json字符串解析为JSON对象,然后就可以了。

答案 1 :(得分:0)

我不知道这是什么原因,但你真的想做http帖子吗?如果我看得好,你只想打电话给服务。为什么不使用HttpGet?