[英]Error parsing dataorg.json.JSONException: Value <!DOCTYPE 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
我正在嘗試將博客博客內容獲取到android應用程序中,為此,我使用的是Blogger API v3,每當我嘗試在android應用程序中獲取JSON對象時,我都會收到錯誤消息“無法將值java.lang.String類型的值轉換為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);
}
}
}
JSONParser:
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.