我是Retrofit 2.0的新手,過去幾天一直試圖解決這個問題,但沒有任何進展。我有一個使用Retrofit 2.0的工作代碼,以便通過使用PHP腳本從SQL DB獲取JSON數據來填充Android活動的回收站視圖。
已經看了許多不同的崗位和教程後,我已經修改了上述工作的代碼,以使一個日期選擇器的選擇的日期通過改造的被髮送到PHP的腳本GET @Query參數改造的界面中在PHP腳本中被$selected_date = $GET["date"];
攔截。
如果我在selected_date_lectures.php中設置了一個值,就像這樣=>$selected_date = "2017-04-22";
根據datePicker的選擇將數據成功提取回recyclerview。此外,DatePicker的日期也可以在選擇時使用敬酒成功顯示。
這使我相信我在做出改進Get @Query請求時或者嘗試從PHP使用$GET["date"];
獲取@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 -Lastly,第三行是我在嘗試使用$ GET來獲得日期參數[ 「日」];
<?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);
?>
此時任何幫助,將不勝感激。先謝謝你。
它是'$ _GET',而不是'$ GET':http://php.net/manual/en/reserved.variables.get.php – adhirajsinghchauhan