【问题标题】:Retrofit 2.0 GET @Query parameter NOT passed to PHP's $GET[""];Retrofit 2.0 GET @Query 参数未传递给 PHP 的 $GET[""];
【发布时间】:2017-04-23 13:55:31
【问题描述】:

我是 Retrofit 2.0 的新手,过去几天一直在尝试解决这个问题,但没有任何进展。我有一个使用 Retrofit 2.0 的工作代码,以便使用 PHP 脚本从 SQL DB 获取的 JSON 数据填充 Android 活动的 recyclerview。

在查看了许多不同的帖子和教程之后,我修改了上述工作代码,以便通过 Retrofit 的 GET @Query 参数将 datePicker 选择的日期发送到 PHP 脚本 em> 在 Retrofit 的接口类中,并被 PHP 脚本中的$selected_date = $GET["date"]; 拦截。

如果我像这样在 selected_date_lectures.php 中设置一个值 => $selected_date = "2017-04-22"; 在选择 datePicker 后,数据将成功取回至 recyclerview。此外,DatePicker 的日期也可以在选择时使用 toast 成功显示。

这让我相信在进行 Retrofit Get @Query 请求或尝试使用 $GET["date"]; 从 PHP 获取 @query 参数时我一定做错了,尽管我没有发现我的任何错误代码。

我没有解决这个问题的想法,非常感谢任何建议。

下面

是来自DatePicker的ondataset的代码片段,该方法是在选择的日期触发HTTP请求时。

@Override

public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {

    // For some reason selected month's output is previous month
    Month +=1;

    // Selected date is formated in the same manner as DB's date which will be compared to find a match.
    String date = Year + "-" + String.format("%02d", Month) + "-" + String.format("%02d", Day);

    swipeRefreshLayout.setRefreshing(true);

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    // Selected date from date picker is added to apiService.getSelectedDateLectures(date);

    Call<List<Message>> call = apiService.getSelectedDateLectures(date);

    call.enqueue(new Callback<List<Message>>() {

        @Override
        public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {

            // clear the inbox
            messages.clear();

            swipeRefreshLayout.setRefreshing(false);

            // add all the messages
            // messages.addAll(response.body());

            // TODO - avoid looping
            // the loop was performed to add colors to each message

            for (Message message : response.body()) {
                // generate a random color
                // message.setColor(getRandomMaterialColor("400"));
                messages.add(message);

            }


            mAdapter.notifyDataSetChanged();
            swipeRefreshLayout.setRefreshing(false);

        }

        @Override
        public void onFailure(Call<List<Message>> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show();
            swipeRefreshLayout.setRefreshing(false);
        }


    });

    Toast.makeText(MainActivity.this, date, Toast.LENGTH_LONG).show();
}

ApiClient.java - 这是构造接口的 HTTP 请求的地方

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ApiClient {

public static final String BASE_URL = "http://lankabentara.tech/FC6P01/android_sign_attendance/";

private static Retrofit retrofit = null;

public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
  }
}

ApiInterface.java - 这是编写所有网络请求的地方。

import info.codex.app.model.Message;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ApiInterface {

// HTTP OPERATION => Fetch today's lectures
@GET("today_lectures.php")
Call<List<Message>> getInbox();

// HTTP OPERATION => Fetch selected date's lectures
@GET("selected_date_lectures.php")
Call<List<Message>> getSelectedDateLectures(@Query("date") String date);
}

selected_date_lectures.php -最后,第三行是我尝试使用 $GET["date"];

获取日期参数的地方
<?php
 ini_set('date.timezone', 'Europe/London');

 $selected_date =  $GET["date"];

 $host="localhost"; //replace with database hostname
 $username="user"; //replace with database username
 $password="password"; //replace with database password
 $db_name="db_name"; //replace with database name

 $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
 mysql_select_db("$db_name")or die("cannot select DB");
 //include("user_control.php"); 
 session_start();

 $uid = $_SESSION['user'];

 $sql = "select users.id,
           degree.degree_id,
           degree.degree_title,
           modules.module_id,       
           modules.degree_id,
           modules.title,
           LectureRoom.code,   
           LectureRoom.date,
           LectureRoom.time,
           LectureRoom.end_time
           from LectureRoom
     JOIN modules ON modules.module_id = LectureRoom.module_id
     JOIN degree ON degree.degree_id = modules.degree_id
     JOIN users ON users.degree_id = degree.degree_id
    WHERE users.id = '32' AND DATE(LectureRoom.date)= '$selected_date'; ";

   $result = mysql_query($sql);
   $json = array();

   if(mysql_num_rows($result)){
   while($row=mysql_fetch_assoc($result)){
   $json[]=$row;
   }
  }
mysql_close($con);
echo json_encode($json);
?>

在这一点上,任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

标签: php android mysql get retrofit2


【解决方案1】:

$selected_date = $GET["date"];

应该是$_GET 而不是$GET,所以:

$selected_date = $_GET["date"];

强制 RTM here。在开发过程中也可以考虑error_reporting(E_ALL);。

【讨论】:

  • 非常感谢@Marcin,这个愚蠢的错误是问题所在,它解决了!当 Stackoverflow 允许我时,我会接受你的答案!
猜你喜欢
  • 1970-01-01
  • 2013-06-13
  • 2020-09-11
  • 2018-12-10
  • 2016-10-22
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多