Blogger JSON API Error: Value Not of type java.lang.String cannot be converted to JSONObject

Blogger JSON API Error: Value Not of type java.lang.String cannot be converted to JSONObject

我正在尝试将 Blogger 博客内容提取到 android 应用程序中,为此我正在使用 Blogger API v3,每当我尝试在 [=25= 中提取 JSON 对象时] 应用程序我得到一个错误 "Value Not of type java.lang.String cannot be converted to JSONObject" 下面是我的代码:

主要活动:

public class MainActivity extends Activity {

private TextView tmpText;
private final String api_key = "myApiKey";
private final String blog_id = "myBlogId";
private final String url = "https://www.googleapis.com/blogger/v3/blogs/"+blog_id+"?key="+api_key;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tmpText = (TextView) findViewById(R.id.tempText);

    new Fetch().execute();
}

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

    JSONObject jsonObject;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tmpText.setText("Loading...");
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONParser jsonParser = new JSONParser();
        jsonObject = jsonParser.getJSONFromUrl(url);
        return jsonObject;
    }

    @Override
    protected void onPostExecute(JSONObject object) {
        super.onPostExecute(object);
        tmpText.setText(""+object);
    }
}
}

JSON解析器:

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

// constructor
public JSONParser() {
}

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);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON String
    return jObj;
}
}

这是 JSON 通过浏览器获取时的响应:

{
    "kind": "blogger#blog",
    "id": "myBlogId",
    "name": "myBlogName",
    "description": "myBlogDesc",
    "published": "myBlogPublishedDate",
    "updated": "myBlogUpdatedDate",
    "url": "myBlogUrl",
    "selfLink": "myBlogSelfLink",
    "posts": {
        "totalItems": 159,
        "selfLink": "myBlogTotalItemsSelfLink"
    },
    "pages": {
        "totalItems": 7,
        "selfLink": "myBlogTotalPagesSelfLink"
    },
    "locale": {
        "language": "en",
        "country": "",
        "variant": ""
    }
}

错误:

E/JSON Parser﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject

您正在执行 POST 而不是 GET。使用 HttpGet 而不是 HttpPost.