通过volley请求发送时获取JSON响应

时间:2016-01-08 11:07:27

标签: java php android json android-volley

我有php文件,当用户发布现有的ticknumber时,php会回显一个已发布的数组的JSON代码,我将它与Activity链接,它将执行相同的php文件工作但是当我点击按钮没有显示在Listview !!

<?php


    if ($_SERVER ['REQUEST_METHOD']=='POST') {

        $ticketnumber = $_POST['ticketnumber'];

        require_once('config.php');

     $con->set_charset('utf8');

        $sql = " SELECT * FROM contact WHERE ticketnumber = '$ticketnumber' ";

        $res = mysqli_query($con, $sql);

         $result = array();

        while($get = mysqli_fetch_array($res))
        {
            array_push($result,array('ticketnumber' =>$get[5], 'subject' =>$get[4],'response' =>$get[6]));
        }
        if(!empty($result)){
            echo json_encode(array("responseticket"=>$result));
        } else {
            echo " error";
        }

}
?>

SupportActivity.java

public class supportActivity extends AppCompatActivity implements View.OnClickListener{

private EditText ticketsupport;
private Button button;
private List<supportContent> con = new ArrayList<supportContent>();
private ListView supportlist;
private supportAdapter adapter;
private String ticketinput;
private String url = "http://10.0.3.2/aaa/getticket.php";
private RequestQueue requestQueue1;
int i ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_support);

     ticketsupport = (EditText)findViewById(R.id.insertticketnumber);
     supportlist = (ListView)findViewById(R.id.supportlistview);
     adapter = new supportAdapter(this, con);
    supportlist.setAdapter(adapter);
     button = (Button)findViewById(R.id.buttonsupprt);

    button.setOnClickListener(this);


}

private void inquiry() {

         ticketinput = ticketsupport.getText().toString().trim();

    StringRequest stringRequest1 = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    if (response.trim().equals("error")) {

                        Toast.makeText(supportActivity.this, "please check the number", Toast.LENGTH_SHORT).show();
                    } else {


                        try {
                            JSONObject ticket ;
                            JSONArray jsonArray = new JSONArray("responseticket");
                            ticket = jsonArray.getJSONObject(Integer.parseInt(response));
                            supportContent support = new supportContent();

                            support.setTicketnumber(ticket.getString("ticketnumber"));
                            support.setSubject(ticket.getString("subject"));
                            support.setResponse(ticket.getString("response"));

                            con.add(support);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();

                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(supportActivity.this, "something wrong" , Toast.LENGTH_SHORT).show();
        }
    }) {

        @Override
    protected Map<String,String> getParams() throws AuthFailureError{
            Map<String,String> map = new HashMap<String,String>();
            map.put("ticknumber", ticketinput);
            return map;
        }
    };

     requestQueue1 = Volley.newRequestQueue(getApplicationContext());
    requestQueue1.add(stringRequest1);

}

@Override
public void onDestroy(){
    super.onDestroy();
}


@Override
    public void onClick(View view){
        inquiry();
 }
}

SupportAdapter.java

public class supportAdapter extends ArrayAdapter<supportContent> {

private Context context;
private List<supportContent> contents;

public supportAdapter(Context context, List<supportContent> con){
    super(context, R.layout.supportcontent, con);
    this.contents = con;
}

@Override
public int getCount(){
    return contents.size();
}

public View getView(int position, View convertview, ViewGroup parent){

    LayoutInflater inflater = LayoutInflater.from(getContext());

    View supcon = inflater.inflate(R.layout.supportcontent, null);

    TextView ticketnumber = (TextView)supcon.findViewById(R.id.ticketnumber);
    ticketnumber.setText(contents.get(position).getTicketnumber());
    TextView supportsubject = (TextView)supcon.findViewById(R.id.supportsubject);
    supportsubject.setText(contents.get(position).getSubject());
    TextView response = (TextView)supcon.findViewById(R.id.response);
    response.setText(contents.get(position).getResponse());

    return supcon;
}
}

1 个答案:

答案 0 :(得分:0)

解析您的回复,如下所示 -

JSONObject jObj = new JSONObject(response);
JSONArray jResponseTicketarray = jObj.getJSONArray("responseticket");

JSONObject jTicket = jResponseTicketarray.getJSONObject(0); 
String Ticketnumber = jTicket.getString("ticketnumber");
String Subject = jTicket.getString("subject");
String Response = jTicket.getString("response");
  • 响应 - 在onResponse()mth返回Json响应。

  • jResponseTicketarray.getJSONObject(0); - 这里'0'是ResponseTicketArray中的第一个元素。如果der是多个对象,你可能想迭代thr循环并提取必需的字段。

修改以下代码 -

supportContent support = new supportContent();

support.setTicketnumber(ticket.getString("ticketnumber"));
support.setSubject(ticket.getString("subject"));
support.setResponse(ticket.getString("response"));

到下面 -

supportContent support = new supportContent();

support.setTicketnumber(TicketNumber);
support.setSubject(Subject);
support.setResponse(Response);