【问题标题】:How to send parameter with GET request using retrofit2 in android?如何在android中使用retrofit2发送带有GET请求的参数?
【发布时间】:2020-08-14 02:13:06
【问题描述】:

Gud 晚上给所有的人... 我是新手,现在正在进行改造 我想要实现的目标 我有一个微调器,现在存在多个学生 我想在与所选学生的新活动中获得考试结果 换句话说,我只想要选择学生的考试结果微调器。 我的微调器网址

http://example.com/web_service/test.php?call=view_children&branch=22&cellno=03016138662

微调器 Json

{
  "content": [
    {
      "Sr": 1,
      "std_name": "Ahmad Nafees",
      "father_name": "Atif Majeed",
      "std_id": 19399,
      "std_img": "https://example.com/EduPortal/Production/students_pics/",
      "std_class": null,
      "std_section": "PSP ",
      "std_class_id": 155,
      "std_section_id": 244
    },
    {
      "Sr": 2,
      "std_name": "Khajida Atif",
      "father_name": "Atif Majeed",
      "std_id": 28684,
      "std_img": "https://example.com/EduPortal/Production/students_pics/",
      "std_class": null,
      "std_section": "Nursery",
      "std_class_id": 156,
      "std_section_id": 245
    }
  ]
}

考试 Json 网址

http://example.comweb_service/test.php?call=view_results&session_id=5&branch=22&student=19399

如您所见,微调器 json 有一个名为 stu_id 的参数,在考试 json url 中,student=19399 所以我想替换学生证来获得选定的学生成绩。我怎样才能做到这一点,或者这可能吗? 提前致谢...

【问题讨论】:

  • 正确放置您的内容
  • 您还在寻找答案还是已解决?

标签: android json get retrofit2 spinner


【解决方案1】:

您需要通过改造进行 POST 请求,因此在后端您将有一个接受此数据的路由。要使用 Retrofit 执行简单的 POST 请求,您可以执行以下操作: 为您的请求创建一个接口

@FormUrlEncoded
    @POST("register_user")
    Call<RegisterUser> registerUser(@Field("name") String name,
                                    @Field("email") String email,
                                    @Field("password") String password);

以及用于检索数据的服务

public void signUpUser(Callback<RegisterUser> callback, String name, String email, String password) {
        Call<RegisterUser> call = apiService.registerUser(name, email, password);
        call.enqueue(callback);
    }

最后进入你的活动:

private void registerUser(final String name, final String email, final String password) {
        UserService userService = new UserService();
        Callback<RegisterUser> callback = new Callback<RegisterUser>() {
            @Override
            public void onResponse(Call<RegisterUser> call, Response<RegisterUser> response) {
                if (response.isSuccessful()) {
                    Log.d("TAG", "user registered" +response.body());
                    mProgressDialog.dismiss();
                    Toast.makeText(SignUpActivity.this, "User registered successfully", Toast.LENGTH_SHORT).show();
                    System.out.println(response.body());
                    Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<RegisterUser> call, Throwable t) {
                Toast.makeText(SignUpActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        };
        userService.signUpUser(callback, name, email, password);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 2018-11-14
    • 2020-09-11
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    相关资源
    最近更新 更多