我正试图从Wordpress文章URL获取Json,注意我在Json中并不擅长,并找到了一些代码,使用这些代码我能够获得帖子等等,但我想要的是从文章网址获取信息,而不是网站的主页面。使用此代码我得到的错误(无值),或者无法获取json,因为代码不正确。
网址:“http://thinkdroid.net/5-ways-to-root-your-device/?json=1”
代码:
public class Task extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... urls)
{
JSONObject json = getJSONFromUrl(URL);
parseJson(json);
return "Something";
}
@Override
protected void onPostExecute(String result)
{
}
}
public JSONObject getJSONFromUrl(String url) {
Log.v("INFO", "Requesting: " + url);
JSONObject jObj = null;
String json = null;
// Making HTTP request
StringBuffer chaine = new StringBuffer("");
try {
java.net.URL urlCon = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlCon
.openConnection();
connection.setRequestProperty("User-Agent", "Universal/2.0 (Android)");
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
//InputStream inputStream;
//if(connection.getResponseCode() >= HttpStatus.SC_BAD_REQUEST)
// inputStream = connection.getErrorStream();
//else
// inputStream= connection.getInputStream();
Log.v("INFO", "ResponseCode: " + connection.getResponseCode());
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(
inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
json = chaine.toString();
Log.v("INFO", "Step 1, got Respons");
try {
jObj = new JSONObject(json);
} catch (Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
public void parseJson(JSONObject json) {
try {
// parsing json object
if (json.getString("status").equalsIgnoreCase("ok")) {
try {
JSONObject post = (JSONObject) getJSONFromUrl(URL);
PostItem item = new PostItem();
String Author = "";
if (post.has("author")) {
Object author = post.get("author");
if (author instanceof JSONArray
&& ((JSONArray) author).length() > 0) {
author = ((JSONArray) author).getJSONObject(0);
}
if (author instanceof JSONObject
&& ((JSONObject) author).has("name")) {
author = (((JSONObject) author)
.getString("name"));
}
Author = author.toString();
}
Log.i("YPPPPPP2","ID " + post.getInt("id") + " TITLE " + Html.fromHtml(post.getString("title"))
.toString() + " Date " + post.getString("date") + " Tags " + ((JSONObject) post.getJSONArray("tags").get(0)).getString("slug") + " Author "+Author + " Content: " + post.getString("content"));
// TODO do we dear to remove catch clause?
try {
boolean thumbnailfound = false;
if (post.has("thumbnail")) {
String thumbnail = post.getString("thumbnail");
if (thumbnail != "") {
item.setThumbnailUrl(thumbnail);
thumbnailfound = true;
}
}
if (post.has("attachments")) {
JSONArray attachments = post
.getJSONArray("attachments");
// checking how many attachments post has and
// grabbing the first one
if (attachments.length() > 0) {
JSONObject attachment = attachments
.getJSONObject(0);
item.setAttachmentUrl(attachment
.getString("url"));
// if we do not have a thumbnail yet, get
// one now
if (attachment.has("images")
&& !thumbnailfound) {
JSONObject thumbnail;
if (attachment.getJSONObject("images")
.has("post-thumbnail")) {
thumbnail = attachment
.getJSONObject("images")
.getJSONObject(
"post-thumbnail");
item.setThumbnailUrl(thumbnail
.getString("url"));
} else if (attachment.getJSONObject(
"images").has("thumbnail")) {
thumbnail = attachment
.getJSONObject("images")
.getJSONObject("thumbnail");
item.setThumbnailUrl(thumbnail
.getString("url"));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}`
你可以帮忙吗?
答案 0 :(得分:2)
你的问题在这一行:
public void parseJson(JSONObject json) {
...
JSONObject post = (JSONObject) getJSONFromUrl(URL);
...
}
您必须使用JSONObject post = json.getJSONObject("post");