【发布时间】:2020-08-11 19:53:01
【问题描述】:
此场景涉及将字符串数据从一个活动传递到另一个活动。我想使用这个变量(useremail)来选择 mysql 数据库中的数据。数据没有从服务器返回任何内容。我正在尝试使用改造来做到这一点。我哪里出错了?
API 接口
public interface ApiInterface2 {
String BASE_URL = "10.0.2.2/uploadmultiple/";
@POST("mylist.php")
Call<List<ImageList>> getImgData(@Query("useremail") String userEmail);}
主类
Call<List<ImageList>> call = apiInterface.getImgData(userEmail);
call.enqueue(new Callback<List<ImageList>>() {
@Override
public void onResponse(Call<List<ImageList>> call, Response<List<ImageList>> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
imageLists = response.body();
adapter = new ListingsAdapter(imageLists, MyListings.this);
////////
});
<?php
include 'dbconfig.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$useremail = $_POST["useremail"];
if(empty($useremail)){echo "UserEmail is null";}
try {
$email = $_REQUEST["useremail"];
// Create connection
$conn = new PDO("mysql:host=$HostName;dbname=$DatabaseName", $HostUser, $HostPass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `tabe1` WHERE `useremail` = $email");
$stmt->execute();
$data=array();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$data[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($data);
}
catch (PDOException $e) {
print "Connection failed! Please Try Again Or Contact Us: " . $e->getMessage() . "<br/>";
die();
$conn = null;
}
}
?>
【问题讨论】:
-
您在 php.ini 中使用方法“POST”。但在应用程序中使用了 GET。请像在 PHP 中一样进行方法调用
-
我已经尝试过 Post 和 get 但它不起作用
-
尝试使用`@FormUrlEncoded @POST("mylist.php") 调用
- > getImgData(@Field("useremail") String userEmail);`
-
你如何从 api 调用返回数据,你能告诉我或显示代码吗?
-
我认为如果您在改造中使用 POST 方法,那么您需要将数据作为字段传递而不是使用查询
标签: java android mysql retrofit