如何捕获响应数组然后在Listview上设置? 如何以数组形式获取响应并在Android中的listview上设置?

I already post that problem but nobody provide solution even i edit again my post, so then i delete & post again.

I don't much knows the working of StringRequest Class & its response. please help me by a simple & proper guide.it response a single Jsonobject but not array. In this json response an array of data I don't knows how to catch that array & then set on Listview

server response

I/System.out:
 {
 "success":true,"first_name":"savha","last_name":"jiii","mobile":2147483647
 }
 W/System.err: org.json.JSONException:
 Value
 {
 "success":true,"first_name":"savha","last_name":"jiii","mobile":2147483647
 }
 of type org.json.JSONObject cannot be converted to JSONArray

Json StringRequest Volley

php code

< ?php

$con=mysqli_connect(XXXXXXXXXXXXXX);

$state=$_POST["state"];

$dist=$_POST["dist"];

$statement=mysqli_prepare($con,"SELECT first_name,last_name,mobile FROM first        WHERE state=? AND dist=?");

mysqli_stmt_bind_param($statement,"ss",$state,$dist);

mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);

mysqli_stmt_bind_result($statement,$fname,$lname,$mobile);

$response=array();

$response["success"]=false;

while(mysqli_stmt_fetch($statement))

{

$response["success"]=true;

$response["first_name"]=$fname;

$response["last_name"]=$lname;

$response["mobile"]=$mobile;

}

echo json_encode($response);

?>

ServiceRequest class

public class SevakRequest  extends StringRequest {

private static final String LOGIN_REQUEST_URL="http://XXXXXX.php";
private Map<String ,String> params;

public SevakRequest(String state,String dist, Response.Listener<String> listener)
{
    super(Method.POST,LOGIN_REQUEST_URL,listener,null);
    params=new HashMap<>();
   // params.put("service",service);
    params.put("state",state);
    params.put("dist",dist);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

MainActivity class

public class SearchResult extends Activity {
Button btn1,btn2;
ListView lv1;
TextView tv1;
String serv, stat, dist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    lv1 = (ListView) findViewById(R.id.lv1);
    tv1 = (TextView) findViewById(R.id.tv1);

    Intent in = getIntent();
    serv = in.getStringExtra("key1");
    stat = in.getStringExtra("key2");
    dist = in.getStringExtra("key3");
    tv1.setText(serv + " >> " + stat + " >> " + dist);

  Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                boolean success = jsonResponse.getBoolean("success");

                if (success) {
                    JSONArray jsonArray = new JSONArray(response);
                    int length = jsonArray.length();
                    List<String> listContents = new ArrayList<String>(length);
                    for (int i = 0; i < length; i++) {
                        String fname = jsonResponse.getString("first_name").toString();
                        String lname = jsonResponse.getString("last_name").toString();
                        String mobile = jsonResponse.getString("mobile").toString();
                        listContents.add("First name: " + fname + "
Last name: " + lname + "
Mobile: " + mobile);
                    }
                    lv1.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listContents));

                } else {
                    Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
    SevakRequest sevakRequest = new SevakRequest(stat, dist, listener);
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    queue.add(sevakRequest);
}
}

first of all ensure your json data is the correct one. I guess you want this:

{
  "success": true,
  "data": [{
    "first_name": "savha",
    "last_name": "jiii",
    "mobile": 2147483647
  }, {
    "first_name": "john",
    "last_name": "smith",
    "mobile": 333123456
  }]
}

For now just return a static json file, so you can focus on developing your app. Than you can change your java code like JSONArray arr = jsonResponse.getJSONArray("data"); to get the array of contact Objects.

Personaly I prefere to use a lib like Gson for easier json en-/decode.