Getting String from Json?
I have a strange problem in my android app. In one method I do this :
try {
String r = responseBody.toString();
JSONArray jArray = new JSONArray(r);
categorys = new String[jArray.length()];
idcategory = new Integer[jArray.length()];
System.out.println("lung " + jArray.length());
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsdata = jArray.getJSONObject(i);
String idcat = jsdata.getString("id_category");
idcategory[i] = Integer.valueOf(idcat);
System.out.println("Id " + idcat);
String namecategory = jsdata.getString("category_name");
categorys[i] = namecategory;
System.out.println("Category name " + namecategory);
}
and everything works fine, I get from server the categorys and category's id. In an other method I do this (for an other response) :
try {
String re = responseBody.toString();
JSONArray jArray = new JSONArray(re);
System.out.println("lung " + jArray.length());
titlephotos = new String[jArray.length()];
photolink=new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsda开发者_如何学Pythonta = jArray.getJSONObject(i);
String titlephoto = jsdata.getString("title");
System.out.println("titlu photo " + titlephoto);
titlephotos[i] = titlephoto;
String linkphoto=jsdata.getString("view");
System.out.println("link photo"+ linkphoto);
photolink[i]=linkphoto;
}
}catch(JSONException e) {
System.out.println("You are in catch");
}
and I get only one title photo an after that I get the message from catch(). If I don't put
String linkphoto=jsdata.getString("photo link");
System.out.println("link photo"+ linkphoto);
photolink[i]=linkphoto;
I get all the titles. I don't understand where is the problem,because the methods are similar, and the first one works fine. Can anyone help?
Thanks...
Most likely it is the space in String linkphoto=jsdata.getString("photo link");
精彩评论