我想从JSON填充微调器。我在log cat上有正确的打印响应。但是没有在Spinner中获取。这是我的json格式
JSON:
[
{
"cat_id": "1",
"cat_code": "ELEC1",
"cat_name": "Electronics",
"cat_created_date": "2014-08-28",
"cat_isactive": "Y",
"cat_updated_date": "0000-00-00"
},
{
"cat_id": "2",
"cat_code": "Decor1",
"cat_name": "Decor",
"cat_created_date": "0000-00-00",
"cat_isactive": "Y",
"cat_updated_date": "0000-00-00"
}
]
我想在Spinner中显示cat_name 但我得到JSONException 有人可以帮我解决这个问题。
这是我的鳕鱼
public class Customer_Order_Detail extends Activity
{
private ArrayList<Category> categoriesList;
ProgressDialog pDialog;
Spinner spinnerCategory;
// Url to get all categories
private String URL_CATEGORIES = "http://192.168.1.102/client_vendor_mgmt/category.php";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_order);
categoriesList = new ArrayList<Category>();
spinnerCategory = (Spinner)findViewById(R.id.spinnerCategory);
new GetCategories().execute();
}
/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < categoriesList.size(); i++)
{
lables.add(categoriesList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerCategory.setAdapter(spinnerAdapter);
}
/*
* Async task to get all categories
*/
private class GetCategories extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Customer_Order_Detail.this);
pDialog.setMessage("Fetching categories..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", " > " + json);
if (json != null) {
try
{
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null)
{
JSONArray categories = jsonObj.getJSONArray("category_master");
for (int i = 0; i < categories.length(); i++)
{
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("cat_id"),catObj.getString("cat_code"));
categoriesList.add(cat);
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else
{
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner();
}
}
}
Here is my log cat info
08-30 13:11:02.453: E/Response:(640): >
08-30 13:11:02.453: E/Response:(640): [{"cat_id":"1","cat_code":"ELEC1","cat_name":"Electronics","cat_created_date":"2014-08-28","cat_isactive":"Y","cat_updated_date":"0000-00-00"},{"cat_id":"2","cat_code":"Decor1","cat_name":"Decor","cat_created_date":"0000-00-00","cat_isactive":"Y","cat_updated_date":"0000-00-00"}]
08-30 13:11:02.563: W/System.err(640): org.json.JSONException: Value [{"cat_created_date":"2014-08-28","cat_code":"ELEC1","cat_id":"1","cat_updated_date":"0000-00-00","cat_isactive":"Y","cat_name":"Electronics"},{"cat_created_date":"0000-00-00","cat_code":"Decor1","cat_id":"2","cat_updated_date":"0000-00-00","cat_isactive":"Y","cat_name":"Decor"}] of type org.json.JSONArray cannot be converted to JSONObject
08-30 13:11:02.563: W/System.err(640): at org.json.JSON.typeMismatch(JSON.java:107)
08-30 13:11:02.563: W/System.err(640): at org.json.JSONObject.<init>(JSONObject.java:158)
08-30 13:11:02.573: W/System.err(640): at org.json.JSONObject.<init>(JSONObject.java:171)
08-30 13:11:02.573: W/System.err(640): at com.customer.demo.Customer_Order_Detail$GetCategories.doInBackground(Customer_Order_Detail.java:98)
08-30 13:11:02.623: W/System.err(640): at com.customer.demo.Customer_Order_Detail$GetCategories.doInBackground(Customer_Order_Detail.java:1)
08-30 13:11:02.623: W/System.err(640): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-30 13:11:02.623: W/System.err(640): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-30 13:11:02.623: W/System.err(640): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-30 13:11:02.633: W/System.err(640): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-30 13:11:02.633: W/System.err(640): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-30 13:11:02.633: W/System.err(640): at java.lang.Thread.run(Thread.java:1019)
答案 0 :(得分:1)
错误是只读。 根值是JSON数组(不是JSON对象)。因此,代码应该使用JSONArray而不是JSONObject;
JSONArray categories = new JSONArray(json);
然后根据需要循环遍历JSONArray(类别)。
答案 1 :(得分:0)
你发布了JSONArray并且你正在解析为JSONObject所以它给出了错误所以试试这个
JSONArray categories = new JSONArray(json);
JSONObject catObj = new JSONObject();
for (int i = 0; i < categories.length(); i++){
catObj = categories.get(i);
Category cat = new Category(catObj.getInt("cat_id"),
catObj.getString("cat_code"));
categoriesList.add(cat);
}