简体   繁体   中英

Can't send data from android app to server

I made this code in my app :

public class human_Adding_Items extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_human__adding__items);
    final EditText humanButtonadd1 = (EditText) findViewById(R.id.imageButton1);
    final EditText humanButtonadd2 = (EditText) findViewById(R.id.imageButton2);
    final Button humanButtonadd3 = (Button) findViewById(R.id.imageButton3);
    final Button humanButtonadd4 = (Button) findViewById(R.id.imageButton4);
    final Button humanButtonadd5 = (Button) findViewById(R.id.imageButton5);
    final Button humanButtonadd6 = (Button) findViewById(R.id.imageButton6);

    humanButtonadd3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            humanButtonadd3.setBackgroundResource(R.mipmap.item_button);
            humanButtonadd4.setVisibility(View.VISIBLE);

        }
    });
    humanButtonadd4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            humanButtonadd4.setBackgroundResource(R.mipmap.item_button);
            humanButtonadd5.setVisibility(View.VISIBLE);

        }
    });
    humanButtonadd5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            humanButtonadd5.setBackgroundResource(R.mipmap.item_button);
            humanButtonadd6.setVisibility(View.VISIBLE);

        }
    });
    humanButtonadd6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            humanButtonadd6.setBackgroundResource(R.mipmap.item_button);

        }
    });
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
    final Date currentLocalTime = cal.getTime();
    final DateFormat date = new SimpleDateFormat("HH:mm a");
    date.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));

    final String localTime = date.format(currentLocalTime);
    final Button b = (Button) findViewById(R.id.ConfirmButtton);
    b.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            final String url = "http://192.168.43.27/app_site/api/post.php";
            StringRequest sq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                protected Map<String, String> getParams(){
                    Map<String, String>  parr = new HashMap<String, String>();
                    parr.put("text1", humanButtonadd1.getText().toString() );
                    parr.put("text2", humanButtonadd2.getText().toString() );
                    return parr;
                }
            };
        }
    });
}

and this my code on server post.php

<?php
$conn = mysqli_connect("localhost","root","","app_db")
or die("Error " . mysqli_error($conn));
$m_text1 = "";
$m_text2 = "";
// recieved from app
$m_text1 = $_POST['text1'];
$m_text2 = $_POST['text2'];
echo "Response: ".$m_text1." ";
$sql_query = "INSERT INTO mytab (text1,text2) VALUES ('$m_text1','$m_text2')";
mysqli_close($conn);
?>

I try to send text data from app, to server, but it happens nothing...:( and i don't understand why?! can u help me pls? btw last file post.php shows the following error :

Notice: Undefined index: text1 in C:\\xampp\\htdocs\\app_site\\api\\post.php on line 13

Notice: Undefined index: text2 in C:\\xampp\\htdocs\\app_site\\api\\post.php on line 14

but it steel insert text1 and text2 in database on refreshing page PS im useing local server "xampp", maybe this is the problem?

use volley to communicate with server and database just add this

in you main gradle file add

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

then in your app gradle file add this in your dependecy

compile 'com.mcxiaoke.volley:library:1.0.19'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'

this is trusted you can follow the tutorial google to get started

https://developer.android.com/training/volley/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM