duangan6133 2018-11-29 03:43
浏览 52

从Android应用程序发送数据到服务器时出错

I am sending data from android app to PHP server and i have got the errors:

Notice: Undefined index: name in E:\xampp\htdocs\server.php on line 4

Notice: Undefined index: email in E:\xampp\htdocs\server.php on line 5

I have taken two edit text and data enter in the edit text should be sent to the server while the button is clicked. So I have made a method for this purpose and I call it in the button on clicked listener

I don't know why I am not receiving data in server there is no problem in my code Please help me!

My client code is

public class MainActivity extends AppCompatActivity {

    EditText ed_name, ed_email;
    Button btn;
    String url = "http://xxxxxxx/server.php";//hidden

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed_name=(EditText)findViewById(R.id.editText);
        ed_email=(EditText)findViewById(R.id.editText2);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
    }

    public void insert() {


        StringRequest MyRequest = new StringRequest(Request.Method.POST, url,

                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {

                        Toast.makeText(getApplication(), response, Toast.LENGTH_SHORT).show();
                        //Toast.makeText(MainActivity.this,"There is some error on server side",Toast.LENGTH_LONG).show();
                    }

                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(MainActivity.this, error + "", Toast.LENGTH_LONG).show();
            }
        }) {
            protected Map<String, String> getparams() throws AuthFailureError {
                Map<String, String> mydata = new HashMap<String, String>();
                String name=ed_name.getText().toString();
                String email=ed_email.getText().toString();
                mydata.put("name", name);
                mydata.put("email", email);
                return mydata;
            }
        };
        RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
        MyRequestQueue.add(MyRequest);
    }
}

While Server Side Code is:

<?php
    $name = $_POST['name'];//i am retrieving data where ['name]' is key 
    $email = $_POST['email'];//Here ['email '] is the key i have given in android code

    echo $name;
    echo $email;
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 制裁名单20240508芯片厂商
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致