如何? -在同一文件执行GET之前,将字符串插入php文件

时间:2019-05-23 09:52:57

标签: java php android

我已成功将我的android应用程序连接到mysql数据库,并从数据库中读取了内容,该数据库显示了该应用程序中一个表的所有行。我想缩小范围,使其仅显示与用户ID对应的行。从用户登录应用程序开始,我将它们存储为共享首选项。我需要我的php文件来识别用户ID,然后将该ID用作查询的一部分,因此它仅显示适当的行。我在尝试设置此代码时遇到了麻烦,并且会从一些帮助中受益。请参见下面的php和java。

PHP代码:

<?php
include('conn.php');

if(isset($_GET['userId'])){//The PHP file doesnt work with these two lines
$getId = $_GET['userId'];

$query = "SELECT * FROM Cbt WHERE userId = '$getId'";
$result = mysqli_query($conn, $query);
$json_array = array();
while ($row = mysqli_fetch_assoc($result)){
$json_array[] =$row;
}
}
echo json_encode($json_array);

JAVA代码:

  loginPref = getSharedPreferences("loginPref", Context.MODE_PRIVATE);
    final int userId = loginPref.getInt("userId", 0);

    textViewResult = findViewById(R.id.text_viewer_result);
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    Api api = retrofit.create(Api.class);
    Call<List<WorkoutLogRecycler>> call = api.getLogs();
    call.enqueue(new Callback<List<WorkoutLogRecycler>>() {
        @Override
        public void onResponse(Call<List<WorkoutLogRecycler>> call,   Response<List<WorkoutLogRecycler>> response) {

            if (!response.isSuccessful()) {
                textViewResult.setText("Code:"+response.code());
                return;
            }

            List<WorkoutLogRecycler> workoutLogRecyclers =  response.body();
            for (WorkoutLogRecycler workoutLogRecycler : workoutLogRecyclers){
                String content ="";
                content += "cbtId: " + workoutLogRecycler.getCbtId() +"\n";
                content += "userId: " + workoutLogRecycler.getUserId() +"\n";
                content += "moodBefore: " + workoutLogRecycler.getMoodBefore() +"\n";
                content += "automaticThought: " + workoutLogRecycler.getAutomaticThought() +"\n";
                content += "distortions: " + workoutLogRecycler.getDistortions() +"\n";
                content += "challengeTought: " + workoutLogRecycler.getChallengeThought() +"\n";
                content += "alternativeThought: " + workoutLogRecycler.getAlternativeThought() +"\n";
                content += "moodAfter: " + workoutLogRecycler.getMoodAfter() +"\n";
                textViewResult.append(content);
            }
        }


        @Override
        public void onFailure(Call<List<WorkoutLogRecycler>> call,  Throwable t) {
        textViewResult.setText(t.getMessage());
        }
    });

API代码:

公共接口Api {

@FormUrlEncoded
@POST("insert.php")
Call<ResponseBody> insertLog(
        @Field("userId") int userId,
        @Field("moodBefore") int moodBefore,
        @Field("automaticThought") String automaticThought,
        @Field("distortions") int distortions,
        @Field("challengeThought") String challengeThought,
        @Field("alternativeThought") String alternativeThought,
        @Field("moodAfter") int moodAfter
);


@GET("read.php")
Call<List<WorkoutLogRecycler>> getLogs();

2 个答案:

答案 0 :(得分:1)

好,因此您的API网址getLogs()需要一个将传递到php脚本的参数

@GET("read.php")
Call<List<WorkoutLogRecycler>> getLogs(@Query("userId") String userId);

然后更改行

Call<List<WorkoutLogRecycler>> call = api.getLogs();
// to
Call<List<WorkoutLogRecycler>> call = api.getLogs(userId);

检查是否有效。基本上,您执行.baseUrl()中提供的请求,但不附加任何参数。在服务器端使用GET时,URL应该包含一些附加数据。像:https://www.api.com?userId=2

然后$_GET['userId']可以从URL中提取userId值。

使用@Query注释为您添加改造附加参数。

答案 1 :(得分:0)

从PHP的第一眼看,我可以知道您没有在查询中正确传递$getId。您应该做的是:$query = "SELECT * FROM Cbt WHERE userId = " . $getId;

除此之外,通过为它们分配变量来检查this way是否使用mysqli查询。