【问题标题】:Android Retrofit Fetch Data Based On User InputAndroid Retrofit 根据用户输入获取数据
【发布时间】: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


【解决方案1】:

这个问题最终通过很好地处理 API 端来解决。感谢所有的受访者。

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


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

【讨论】:

    【解决方案2】:

    我认为应该是这样,BASE_URL 应该只在 retrofit 中包含 URL 而不是路径。

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

    这应该对你有帮助。

    【讨论】:

    • 不,这不是真的。 BASE_URL 可以是任何东西。脚本的路径也是一个 URL,可以在基本 url 中传递
    • 请您添加您的错误日志
    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 2016-09-25
    • 2023-04-04
    • 2021-08-28
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多