我正在尝试使用PHP文件从Android(Xampp)数据库获取数据。我在String变量中获得了所需的数据,但似乎我的方法没有进入for循环。以下是我的代码。
@SuppressLint("NewApi")
protected void onPostExecute(String result){
//parse json data
try {
System.out.println("in jsn try");
String s = "";
System.out.println("after jsn string s");
JSONArray jArray = new JSONArray(result);
System.out.println("after creating json array "+result);// it executes fine and prints all records
System.out.println("jarray lenght is "+jArray.length());// it execute fine and prints the length 3 which is fine
for (int i = 0; i < jArray.length(); i++) { // problem here
System.out.print("in jsn loop");
JSONObject json = jArray.getJSONObject(i);
System.out.print("json object");
s = s +"Name : "+json.getString("Name")+" "+json.getString("Email");
}
System.out.print("Success in json" +s);
// resultView.setText(s);
}
catch (Exception e) {
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
响应变量打印3条记录
[
{
"PId":"1111",
"Name":"Rukhsar",
"Email":"rukhsar_arshad93@yahoo.com",
"Address":"Rawalpindi"
},
{
"PId":"2222",
"Name":"Sidra",
"Email":"sidra_arshad@yahoo.com",
"Address":"Rawalpindi"
},
{
"PId":"3333",
"Name":"Sehrish ",
"Email":"sehrishali686@yahoo.com",
"Address":"Islamabad"
}
]
答案 0 :(得分:0)
这可以解决您的问题
public class User {
private String Pid;
private String name;
private String email;
private String address;
//implement getter setters
//implement to string method
}
public void parseJSON(String result) {
try {
JSONArray jArray = new JSONArray(result);
List<User> usersList = new ArrayList<User>();
for (int i = 0; i < jArray.length(); i++) { // problem here
User user = new User();
JSONObject json = jArray.getJSONObject(i);
user.setPid(json.getString("PId"));
user.setName(json.getString("Name"));
user.setEmail(json.getString("Email"));
user.setAddress(json.getString("Address"));
Log.i("User data is : ",User.toString);
usersList.add(user);
}
}
catch (Exception e) {
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
答案 1 :(得分:0)
@SuppressLint("NewApi")
protected void onPostExecute(String result){
//parse json data
try {
System.out.println("in jsn try");
String s = "";
System.out.println("after jsn string s");
JSONArray jArray = new JSONArray(result);
System.out.println("after creating json array "+result);// it executes fine and prints all records
System.out.println("jarray lenght is "+jArray.length());// it execute fine and prints the length 3 which is fine
for (int i = 0; i < jArray.length(); i++) { // problem here
System.out.print("in jsn loop");
JSONObject json = jArray.getJSONObject(i);
System.out.print("json object");
s = s +"Name : "+json.getString("Name")+" "+json.getString("Email");
}
System.out.print("Success in json" +s);
// resultView.setText(s);
}
catch (Exception e) {
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
我看到这段代码存在一些问题。首先,您使用System.out.print
代替System.out.println
。更好的是,因为您正在使用Android,所以使用Log.w(String, String)
。还
s = s +"Name : "+json.getString("Name")+" "+json.getString("Email");
你应该附加换行符:
String line = "Name : "+json.getString("Name")+" "+json.getString("Email");
s += line + "\n"; //so it starts on a newline
Log.d("json", line); //while you're at it, Log that line!
<强>加成强>:
你应该使用调试器!在for循环中添加断点,并在调试模式下运行! Eclipse中的标准功能也适用于Android。
修改强>
所以我对你的代码的看法是:
private static final String NAME = "Name";
private static final String EMAIL = "Email";
@SuppressLint("NewApi")
protected void onPostExecute(String result) {
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject user = array.getJSONObject(i);
resultView.append("Name : " + user.getString(NAME) + " - " + user.getString(EMAIL) + "\n");
}
}
catch (JSONException jse) {
Log.wtf("error", "cant parse json"); //yeah, Log.wtf actually exists :)
}
}