gpt4 book ai didi

android - Restful API : Unable to get information from Wordpress Website

转载 作者:行者123 更新时间:2023-11-30 01:56:51 24 4
gpt4 key购买 nike

我试图从我的 wordpress 网站获取内容。我想要指定帖子中的内容,您可以在那里看到:(理解,这个网站不是真实的,但它来自真实的网站,我只是替换了不同的标题、url 等)http://pastebin.com/PWuC8usi

我正在使用 Wordpress API (RESTFUL API)。

错误说:

Error parsing data Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream inputStream = null;
String result= null;
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);

try {

HttpResponse response = client.execute(httpGet);
inputStream = response.getEntity().getContent();

// convert inputstream to string
if(inputStream != null){
result = convertInputStreamToString(inputStream);
Log.i("App", "Data received:" +result);

}
else
result = "Failed to fetch data";

return result;

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;
}



private void parseJSON(String data){

try{
JSONObject jsonResponse = new JSONObject(data);
//JSONArray jsonMainNode = jsonResponse.getJSONArray("posts");
JSONArray jsonMainNode = jsonResponse.getJSONArray("content");
Log.i("App", "jsonMainNode = "+jsonMainNode);

int jsonArrLength = jsonMainNode.length();
Log.i("App", "jsonArrLength = "+jsonArrLength);



for(int i=0; i < jsonArrLength; i++) {

JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String postTitle = jsonChildNode.getString("title");
String postUrl = jsonChildNode.getString("url");
String postDate = jsonChildNode.getString("date");
String postContent = jsonChildNode.getString("content");

tvPostTitle.setText("Page title: " +postTitle);
tvPostUrl.setText("Page URL: " +postUrl);
tvPostDate.setText("Date: " +postDate);
tvPostContent.setText("Content: " +android.text.Html.fromHtml(postContent).toString());
}

}catch(Exception e){
Log.i("App", "Error parsing data " +e.getMessage());

}
}

最佳答案

您正在解析的字符串不是 JSON 格式。

以下 fragment :

try {
String data = "<!DOCTYPE not a json data goes here ...";
JSONObject jsonResponse = new JSONObject(data);
} catch (JSONException e) {
Log.d("App", "Error parsing data " + e.getMessage());
}

会给你同样的错误信息:

Error parsing data Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

我猜您正在连接到错误的 API 端点。检查您正在使用的 URL。使用您自己的网络浏览器连接到那里。

关于android - Restful API : Unable to get information from Wordpress Website,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32084693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com