使用volley将数据从android发送到php

时间:2016-08-25 14:44:25

标签: php android

发送到服务器端的变量似乎无法到达那里,我只是无法弄清问题是什么。我从PHP脚本返回一个回复,说POST是空的。

它应该如何工作

  • 在警告对话框中插入评论
  • 必须将id和评论发布到服务器端

请帮助!!!

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                /* Alert Dialog Code Start*/
                AlertDialog.Builder alert = new AlertDialog.Builder(context);

                final String id_number =((TextView)view.findViewById(R.id.textViewIdnum)).getText().toString();

                alert.setTitle("Alert id: "+id_number); //Set Alert dialog title here
                alert.setMessage("Enter Your Comment here"); //Message here

                // Set an EditText view to get user input
                final EditText input = new EditText(context);
                alert.setView(input);

                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // convert the input to a string and show in a toast.
                        String reason = input.getEditableText().toString();
                        Toast.makeText(SearchActivity.this,"Commenting...", Toast.LENGTH_LONG).show();

                        final String reason_key = input.getText().toString().trim();
                        //final String id_number_key = textview;

                        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.RESULT_URL,

                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        Toast.makeText(SearchActivity.this,response,Toast.LENGTH_LONG).show();
                                    }
                                },
                                new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Toast.makeText(SearchActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                                    }
                                });
                        Map<String,String> params = new HashMap<String, String>();
                        params.put(TAG_IDNUM,id_number);
                        params.put(reason_key,reason);

                        RequestQueue requestQueue = Volley.newRequestQueue(SearchActivity.this);
                        requestQueue.add(stringRequest);

                    } // End of onClick(DialogInterface dialog, int whichButton)
                }); //End of alert.setPositiveButton
                alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                        Toast.makeText(SearchActivity.this,"Commenting cancled",Toast.LENGTH_LONG).show();
                        dialog.cancel();
                    }
                }); //End of alert.setNegativeButton
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
   /* Alert Dialog Code End*/
                // End of onClick(View v)


            }




        });}
catch (JSONException e){}
}

1 个答案:

答案 0 :(得分:0)

首先你需要测试你的php代码是否正常工作,使用postman来测试你的&#34; api&#34;如果它适用于邮递员,你的代码应该有效。

在我看来,它更像是一个PHP失败而不是一个Android失败。

你的

 Toast.makeText(SearchActivity.this,response,Toast.LENGTH_LONG).show();

打印什么?