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

时间:2017-02-14 07:23:16

标签: php android json listview android-volley

我已经发布了这个问题但是没有人提供解决方案,即使我再次编辑我的帖子,所以我删除&再次发帖。

我不太了解StringRequest Class&它的回应。请帮我一个简单的&适当的guide.it响应单个Jsonobject但不响应数组。 在这个json响应中,我不知道如何捕获该数组&然后在Listview上设置

服务器响应

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类

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类

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 + "\nLast name: " + lname + "\nMobile: " + 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);
}
}

1 个答案:

答案 0 :(得分:0)

首先确保您的json数据是正确的。我想你想要这个:

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

现在只需返回一个静态json文件,这样您就可以专注于开发应用程序了。 您可以更改JSONArray arr = jsonResponse.getJSONArray("data");之类的Java代码,以获取联系人对象的数组。

Personaly我更喜欢使用像Gson这样的lib来更容易进行json en- / decode。