![](/img/trans.png)
[英]Use Volley Library for send and get data from server (Android - PHP)
[英]send id then get registration data with volley and php
我用排球法。 我想通过php发布ID,然后从我的服务器数据库中获取注册数据。
StringRequest stringRequest=new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
Toast.makeText(turbine.this, "تبریک", Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(turbine.this, "خطای اتصال به شبکه", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params=new HashMap<String, String>();
params.put("ID", idExtra);
return params;
}
};
StringRequest stringRequest2=new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array=new JSONArray(response);
for (int i=0; i < array.length(); i++) {
JSONObject product=array.getJSONObject(i);
String s1=product.getString("section");
EditText q1=findViewById(R.id.alib1);
q1.setText(s1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
com.android.volley.RequestQueue requestQueue=Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Volley.newRequestQueue(this).add(stringRequest2);
的PHP:
<?php
include 'dbconfig.php';
$ID = $_POST['ID']; ;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM turbine_table where id = $ID" ;
$result = $conn->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>
实际上,我的“ volley post method”应该将ID发布到php,然后“ volley get method”应该从php获取数据。 但它没有:(。。我认为我的问题是有两个截击请求,我认为这是来自php的已发布ID,因为当我在$ ID中设置一个数字时,它可以工作。
我找到了 。 我错误的方式。 我必须以“ post method”作为响应来获取数据。 真的不需要使用get方法:)
StringRequest stringRequest=new StringRequest(Request.Method.POST, URLS.supervisor_turbin,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
Toast.makeText(turbine.this, "تبریک", Toast.LENGTH_LONG).show();
try {
JSONArray array=new JSONArray(response);
for (int i=0; i < array.length(); i++) {
JSONObject product=array.getJSONObject(i);
String s1=product.getString("section");
EditText q1=findViewById(R.id.alib1);
q1.setText(s1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(turbine.this, "خطای اتصال به شبکه", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params=new HashMap<String, String>();
params.put("ID", idExtra);
return params;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.