Android JSON数据已读取,但无法转换为JSONArray
我正在开发一个可以读取JSON数据的应用程序。Json数据已解析,但未在listview中查看。Logcat说类型不匹配。我对Json不太熟悉。Android JSON数据已读取,但无法转换为JSONArray,android,json,arrays,jsonobject,Android,Json,Arrays,Jsonobject,我正在开发一个可以读取JSON数据的应用程序。Json数据已解析,但未在listview中查看。Logcat说类型不匹配。我对Json不太熟悉。 这是我的日志和代码。请给我这个 org.json.JSONException:索引1超出范围[0..1) get(JSONArray.java:263) org.json.JSONArray.getString(JSONArray.java:421) com.is.parsej.parsej$GetContacts.doInBackground(pa
这是我的日志和代码。请给我这个 org.json.JSONException:索引1超出范围[0..1) get(JSONArray.java:263) org.json.JSONArray.getString(JSONArray.java:421) com.is.parsej.parsej$GetContacts.doInBackground(parsej.java:141) com.is.parsej.parsej$GetContacts.doInBackground(parsej.java:1) android.os.AsyncTask$2.call(AsyncTask.java:287) java.util.concurrent.FutureTask.run(FutureTask.java:234) android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
公共类ParseJ扩展ListActivity{
私人对话;
//获取联系人JSON的URL
专用静态字符串url=”http://api.openweathermap.org/data/2.5/forecast/daily?lat=6.421465&lon=81.332396&cnt=10&mode=json";
//JSON节点名称
私有静态最终字符串标记\u COd=“list”;//已编辑
私有静态最终字符串标记_ID=“dt”;//已编辑
私有静态最终字符串标记_WEATHER=“WEATHER”;
私有静态最终字符串标记_MAIN=“MAIN”;
私有静态最终字符串标记_DESC=“description”;
私有静态最终字符串标记_TEMP=“TEMP”;
私有静态最终字符串标记_DAY=“DAY”;
私有静态最终字符串标记_MIN=“MIN”;
私有静态最终字符串标记_MAX=“MAX”;
私有静态最终字符串标记_NIGHT=“NIGHT”;
私有静态最终字符串标记_MORN=“MORN”;
专用静态最终字符串标签\u湿度=“湿度”;
//联系JSONArray
JSONArray联系人=null;
//ListView的Hashmap
ArrayList联系人列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parse_j);
contactList=新的ArrayList();
ListView lv=getListView();
//单击项目上的Listview侦听器
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//从选定的ListItem获取值
字符串weather=((TextView)view.findViewById(R.id.main))
.getText().toString();
字符串Temp=((TextView)view.findViewById(R.id.Descrption))
.getText().toString();
字符串湿度=((TextView)view.findViewById(R.id.temp))
.getText().toString();
//启动单一联系人活动
Intent in=新的Intent(getApplicationContext(),
活动类);
in.putExtra(标记天气、天气);
额外输入(标签温度、温度);
in.putExtra(标签湿度、湿度);
星触觉(in);
}
});
//调用异步任务以获取json
新建GetContacts().execute();
}
/**
*异步任务类通过HTTP调用获取json
* */
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(ParseJ.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
contacts=jsonObj.getJSONArray(TAG_COd);
//通过所有触点循环
对于(int i=0;i public class ParseJ extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=6.421465&lon=81.332396&cnt=10&mode=json";
// JSON Node names
private static final String TAG_COd = "list"; //edited
private static final String TAG_ID = "dt"; //edited
private static final String TAG_WEATHER = "weather";
private static final String TAG_MAIN = "main";
private static final String TAG_DESC = "description";
private static final String TAG_TEMP = "temp";
private static final String TAG_DAY = "day";
private static final String TAG_MIN = "min";
private static final String TAG_MAX = "max";
private static final String TAG_NIGHT = "night";
private static final String TAG_MORN= "morn";
private static final String TAG_HUMIDITY = "humidity";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parse_j);
contactList = 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 weather = ((TextView) view.findViewById(R.id.main))
.getText().toString();
String Temp = ((TextView) view.findViewById(R.id.Descrption))
.getText().toString();
String Humidity = ((TextView) view.findViewById(R.id.temp))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_WEATHER, weather);
in.putExtra(TAG_TEMP, Temp);
in.putExtra(TAG_HUMIDITY, Humidity);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ParseJ.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
contacts = jsonObj.getJSONArray(TAG_COd);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String humidity = c.getString(TAG_HUMIDITY);
// Phone node is JSON Object
JSONObject temp = c.getJSONObject(TAG_TEMP);
String day = temp.getString(TAG_DAY);
String maxTemp = temp.getString(TAG_MAX);
String minTemp = temp.getString(TAG_MIN);
String morningTemp = temp.getString(TAG_MORN);
//edited
JSONArray weather = c.getJSONArray(TAG_WEATHER);
String main = weather.getString(1);
String desc = weather.getString(2);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_DAY, day);
contact.put(TAG_DESC, desc);
contact.put(TAG_HUMIDITY, humidity);
// adding contact to contact list
contactList.add(contact);
}
} 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(
ParseJ.this, contactList,
R.layout.list_item, new String[] { TAG_DAY, TAG_DESC,
TAG_HUMIDITY }, new int[] { R.id.temp,
R.id.main, R.id.Descrption });
setListAdapter(adapter);
}
}
}
org.json.JSONException: Value 200 at cod of type java.lang.String cannot be converted to JSONArray
contacts = jsonObj.getJSONArray(TAG_COd);
String cod = jsonObj.getJSONString(TAG_COd);
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}]
JSONArray weather = c.getJSONArray(TAG_WEATHER);
String main = weather.getString(1);
String desc = weather.getString(2);
JSONObject weather = c.getJSONArray(TAG_WEATHER).getJSONObject(0);
String main = weather.getString("main");
String desc = weather.getString("description");
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray("list");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
}
contacts = jsonObj.getJSONArray(TAG_COd)