在Post with Volley

时间:2016-01-02 08:38:49

标签: java android arrays json android-volley

我有与MySQL的php文件连接,如果我发布错误的ticketnumber php会回显错误但是如果它正确将回显包含当前ticketnumer的一个数组的json_encode,所以我想在listview中显示结果活动,但没有任何东西出现在吐司的消息!!

数组名称为" responseticket"正如我写的那样

    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;
@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 ticket 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();
 }
}

这是一个适配器

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;
}
}

0 个答案:

没有答案