gpt4 book ai didi

php - Retrofit 2.0 GET @Query 参数未传递给 PHP 的 $GET [""];

转载 作者:行者123 更新时间:2023-11-29 02:45:12 25 4
gpt4 key购买 nike

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

在查看了许多不同的帖子和教程之后,我修改了上述工作代码,以便能够通过 Retrofit 的 GET @Query Parameter 在 Retrofit 的接口(interface)类中并被 $selected_date = $GET["date"]; 拦截在 PHP 的脚本中。

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

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

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

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

@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 - 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()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
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
@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 -Lastly, the third line is where I am attempting to get the date parameter using $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);
?>

此时任何帮助将不胜感激。提前谢谢你。

最佳答案

$selected_date = $GET["date"];

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

$selected_date = $_GET["date"];

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

关于php - Retrofit 2.0 GET @Query 参数未传递给 PHP 的 $GET [""];,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43572056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com