我正在使用android studio尝试使用JsonArrayRequest和Post方法获取一个JsonArray,但是它不起作用(POST方法发送一个空字符串),所以我改用StringRequest,但是仍然没有响应!当我运行程序时,没有任何变化。
我想向数据库发送特定的电子邮件,并返回所有关注此电子邮件的人的姓名和投资类型。
该项目仅用于测试,因此我没有使用listView,并且我具有Internet许可并添加了凌空库。
我的主要活动:
final TextView t1 = (TextView) findViewById(R.id.t1);
final TextView t2 = (TextView) findViewById(R.id.t2);
final RequestQueue followsQuery = Volley.newRequestQueue(MainActivity.this);
StringRequest request = new StringRequest( Request.Method.POST, "http://...", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array=new JSONArray(response);
int i =0;
while (i<array.length()){
JSONObject object = array.getJSONObject(i);
t1.setText(object.getString("Name"));
t2.setText(object.getString("Investment Type"));
i++;
}
} catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> p = new HashMap<String, String>();
p.put("email", "[email protected]");
return p;
}
};
followsQuery.add(request);
}
}
PHP文件:
<?php
require 'connection.php';
$email = $_POST['email'];
//echo $email;
$query = "SELECT Name,InvestmentType FROM investor WHERE Email_I IN (SELECT Email_2 FROM follows WHERE Email_1 = '$email' AND Status =0)";
$result = mysqli_query ($conn,$query);
$response = array ();
while ($row = mysqli_fetch_array ($result)){
array_push ($response,array("Name"=>$row[Name],"InvestmentType"=>$row[InvestmentType]));
}
if(empty($response)){
array_push ($response,array("Name"=>"no data","InvestmentType"=>"no data"));
}
echo json_encode ($response);
?>
最佳答案
您的清单是否具有Internet许可?