Android 根据用户输入改造获取数据

Android Retrofit Fetch Data Based On User Input

此场景涉及将字符串数据从一个 activity 传递到另一个。我想使用此变量 (useremail) 来 select mysql 数据库中的数据。数据未从服务器返回任何内容。我正在尝试使用改造来做到这一点。我哪里会出错?

API 界面

public interface ApiInterface2 {
    String BASE_URL = "10.0.2.2/uploadmultiple/";
    @POST("mylist.php")
    Call<List<ImageList>> getImgData(@Query("useremail") String userEmail);}

主要CLASS

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

?>

我觉得应该是这样的,BASE_URL应该只在[=中包含URL 15=]改造 不是路径。

String BASE_URL = "10.0.2.2/";
@POST("uploadmultiple /mylist.php")

这应该对你有帮助。

通过妥善处理 API 侧,最终解决了这个问题。感谢所有的回答者。

API 界面

public interface ApiInterface2 {
    String BASE_URL = "10.0.2.2/uploadmultiple/";
    @POST("mylist.php")
    Call<List<ImageList>> getImgData(@Query("useremail") String userEmail);}

主要CLASS

  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


    if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    try{ 

    $useremail = $_REQUEST["useremail"];
    include 'dbconfig.php';

    $conn = new PDO("mysql:host=$HostName;dbname=$DatabaseName", $HostUser, $HostPass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    $stmt = $conn->prepare("SELECT * FROM `table1` WHERE `useremail` = '$useremail'"); 
    $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;
}