2017-04-23 72 views
1

我是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); 
?> 

此时任何帮助,将不胜感激。先谢谢你。

+0

它是'$ _GET',而不是'$ GET':http://php.net/manual/en/reserved.variables.get.php – adhirajsinghchauhan

回答

0

$ selected_date = $ GET [“date”];

应该$_GET$GET,所以:

$selected_date = $_GET["date"]; 

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

+1

非常感谢@Marcin,这个愚蠢的错误是这个问题,它修复了它!将接受你的答案时,Stackoverflow会让我! – codixer