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

时间:2017-04-23 13:55:32

标签: php android mysql get retrofit2

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

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

如果我在selected_date_lectures.php中设置了一个值,就像这样=>在datePicker的选择下,$selected_date = "2017-04-22";数据成功返回到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);
?>

此时,我们将非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

  

$ selected_date = $ GET [&#34; date&#34;];

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

$selected_date = $_GET["date"];

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