dtp19819 2017-04-23 13:55
浏览 415

Retrofit 2.0 GET @Query参数未传递给PHP的$ GET [“”];

I am new to Retrofit 2.0 and have been trying to solve this problem for the past few days without any progress. I have a working code which uses Retrofit 2.0 in order to populate an Android activity's recyclerview with JSON data fetched from SQL DB by using a PHP script.

After having looked at many various posts and tutorials, I have modified the above-described working code in order to enable a datePicker's selected date to be sent to PHP's script via Retrofit's GET @Query Parameter within Retrofit's interface class and intercepted by $selected_date = $GET["date"]; inside PHP's script.

If I set a value inside selected_date_lectures.php like this => $selected_date = "2017-04-22"; data is successfully fetched back to the recyclerview upon datePicker's selection. Also the DatePicker's date is also successfully displayed using a toast upon selection.

This makes me believe that I must be doing something wrong when making the Retrofit Get @Query request or when trying to get the @query parameter from PHP using $GET["date"]; although I don't spot anything that could be wrong in my code.

I ran out of ideas to solve this issues and would really appreciate any suggestions.

Below is a code fragment from DatePicker's onDataSet's method which is when HTTP request is triggered upon Date selected.


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


    ApiInterface apiService =

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

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

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

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

            // clear the inbox


            // 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"));




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


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

ApiClient.java - This is where the interface's HTTP request is being constructed

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()
    return retrofit;

ApiInterface.java -This is where all the network requests are written.

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
Call<List<Message>> getInbox();

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

selected_date_lectures.php -Lastly, the third line is where I am attempting to get the date parameter using $GET["date"];

 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");

 $uid = $_SESSION['user'];

 $sql = "select users.id,
           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();

echo json_encode($json);

At this point any help would be much appreciated. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dqm7854 2017-04-23 13:56

    $selected_date = $GET["date"];

    It should be $_GET not $GET, so:

    $selected_date = $_GET["date"];

    Mandatory RTM here. Also consider error_reporting(E_ALL); while you are developing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 初学者用plt报错,求解答
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?