繁体   English   中英

无法将数据从android应用发送到服务器

[英]Can't send data from android app to server

我在我的应用程序中编写了此代码:

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

这是我在服务器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);
?>

我尝试将文本数据从应用程序发送到服务器,但是什么也没发生...:((我不明白为什么?!你能帮我吗?)btw最后一个文件post.php显示以下错误:

注意:未定义的索引:第13行的C:\\ xampp \\ htdocs \\ app_site \\ api \\ post.php中的text1

注意:未定义的索引:第14行的C:\\ xampp \\ htdocs \\ app_site \\ api \\ post.php中的text2

但是它使用本地服务器“ xampp”在刷新页面PS上的数据库中插入了text1和text2,也许是问题所在?

使用凌空与服务器和数据库进行通信,只需添加此

在您的主要gradle文件中添加

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

然后在您的应用程序gradle文件中将此添加到您的dependecy中

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

这是值得信任的,您可以按照Google教程开始

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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