package com.example.velichamjson;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://velicham.co.in/index.php?option=com_obrss&task=feed&id=4:politics&format=json&Itemid=351";
// JSON Node names
private static final String TAG_ITEMS= "items";
private static final String TAG_TITLE= "title";
private static final String TAG_LINK = "link";
private static final String TAG_PUBDATE = "pubDate";
private static final String TAG_DESCRIPTION= "description";
// private static final String TAG_GENDER = "gender";
// private static final String TAG_PHONE = "phone";
// private static final String TAG_PHONE_MOBILE = "mobile";
// private static final String TAG_PHONE_HOME = "home";
// private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray items = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> itemList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String link = ((TextView) view.findViewById(R.id.link))
.getText().toString();
String pubDate = ((TextView) view.findViewById(R.id.pubdate))
.getText().toString();
String description = ((TextView) view.findViewById(R.id.description))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_LINK, link);
in.putExtra(TAG_PUBDATE, pubDate);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});
// Calling async task to get json
new GetItems().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetItems extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
items = jsonObj.getJSONArray(TAG_ITEMS);
System.out.println(items);
// looping through All Contacts
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
String title = c.getString(TAG_TITLE);
String link = c.getString(TAG_LINK);
String pubDate = c.getString(TAG_PUBDATE);
String description = c.getString(TAG_DESCRIPTION);
//String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
// JSONObject phone = c.getJSONObject(TAG_PHONE);
// String mobile = phone.getString(TAG_PHONE_MOBILE);
// String home = phone.getString(TAG_PHONE_HOME);
// String office = phone.getString(TAG_PHONE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> item = new HashMap<String, String>();
// adding each child node to HashMap key => value
item.put(TAG_TITLE, title);
item.put(TAG_LINK, link);
item.put(TAG_PUBDATE, pubDate);
item.put(TAG_DESCRIPTION, description);
// adding contact to contact list
itemList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, itemList,
R.layout.list_item, new String[] { TAG_LINK, TAG_PUBDATE,
TAG_DESCRIPTION ,TAG_TITLE}, new int[] { R.id.link,
R.id.pubdate, R.id.description,R.id.title });
setListAdapter(adapter);
}
}
}
iam试图解析json数据这是一个来自我的json数据的泰米尔新闻但它显示错误没有价值的项目我尝试超过半天任何可以回答它出错的地方我不知道它去哪里错误的人提前感谢我的回复
答案 0 :(得分:0)
UPDATE - 使用JsonObject,您可以执行以下操作
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray arr = new JSONArray(jsonObj.get(TAG_ITEMS));
// looping through All data
for (int i = 0; i < arr.length(); i++) {
JSONObject temp = arr.getJSONObject(i);
String title = temp.get(TAG_TITLE);
String link = temp.get(TAG_LINK);
String pubDate = temp.get(TAG_PUBDATE);
String description = temp.get(TAG_DESCRIPTION);
}
} catch (JSONException e) {
e.printStackTrace();
}
您可以使用JsonReader 像下面的json数据一样,
注意 - 我没有测试过代码,你必须根据你的要求进行调整,但这就是你如何使用androids JsonReader解析JsonData。你可以查看android开发者页面更多信息。
JsonReader reader = new JsonReader(new InputStreamReader(yourResponseStream, Encoding));
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("count")) {
string count = reader.nextString();
} else if (name.equals("value")) {
//Method to process the value object
processValueObject(reader);
} else {
reader.skipValue();
}
}
private void processValueObject(JsonReader reader)
{
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("feedUrl")) {
string feedUrl = reader.nextString();
} else if (name.equals("title")) {
string title = reader.nextString();
} ....and so on
//For array of data
}else if (name.equals("items"))
{
reader.beginArray();
reader.beginObject();
//then use the same methods to get name and value pair.
processReader(reader);
//at the end dont forget to end your array and object
reader.endObject();
reader.endArray();
}
else {
reader.skipValue();
}
}
}
希望它有所帮助...