【发布时间】:2018-05-11 02:38:09
【问题描述】:
我在我的应用中编写了这段代码:
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);
?>
我尝试将文本数据从应用程序发送到服务器,但什么也没发生...:( 我不明白为什么?! 你能帮我吗? 顺便说一句,最后一个文件 post.php 显示以下错误:
注意:未定义的索引:text1 in C:\xampp\htdocs\app_site\api\post.php 在第 13 行
注意:未定义的索引:text2 in C:\xampp\htdocs\app_site\api\post.php 在第 14 行
但它会在刷新页面上的数据库中插入 text1 和 text2 PS我使用本地服务器“xampp”,也许这是问题所在?
【问题讨论】:
-
请求是否从应用程序发送到服务器?
-
您的代码似乎与example 不同。您不会添加到队列中 - 也不会创建它。
-
那个,我不知道它是否与服务器通信,但它不会将文本数据发送到我的数据库:(
-
您的设备是在同一个网络中还是在使用模拟器?因为模拟器使用虚拟网络,默认情况下无法连接到您计算机所在的“真实”网络。
-
是的,我在同一台电脑上使用模拟器 android 和 xampp。
标签: java php android mysql database