如何在 android studio 中通过 POST 方法使用 Secret code 获取 JSON 数据

How to get JSON data by POST method with Secret code in android studio

我需要使用 POST 方法根据密码获取 json 数据响应,请您解决我的问题,在此先感谢。

我一直面临着使用这种 POST 密码方法获取 JSON 响应

的许多问题
 public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listViewHeroes);


    getQuestions();
}

private void getQuestions() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ApiInterface.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
            .build();

    ApiInterface api = retrofit.create(ApiInterface.class);

    RequestModel requestModel = new RequestModel();
    requestModel.setSecretCode("341977082");

    Call<List<ModelObjects>> call = api.getQuestions();

   call.enqueue(new Callback<List<ModelObjects>>() {
       @Override
       public void onResponse(Call<List<ModelObjects>> call, Response<List<ModelObjects>> response) {
           List<ModelObjects> questionsList = response.body();


           String[] questions = new String[questionsList.size()];


           for (int i = 0; i < questionsList.size(); i++) {
               questions[i] = questionsList.get(i).getQues_No();
           }



           listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, questions));
       }

       @Override
       public void onFailure(Call<List<ModelObjects>> call, Throwable t) {
           Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
       }
   });}}

这是界面,我将在其中传递包含父项和扩展名

的 URL
public interface ApiInterface {

String BASE_URL = "";

@POST("QuestionsList")
Call<List<ModelObjects>> getQuestions();}

这是响应模型

 public class ModelObjects {
@SerializedName("Ques_No")
private String Ques_No;

public ModelObjects(String ques_No) {
    Ques_No = ques_No;
}

public String getQues_No() {
    return Ques_No;
}

public void setQues_No(String ques_No) {
    Ques_No = ques_No;
}}

这是请求模型

 public class RequestModel {
private String SecretCode;

public RequestModel(String secretCode) {
    SecretCode = secretCode;
}

public RequestModel() {

}

public String getSecretCode() {
    return SecretCode;
}

public void setSecretCode(String secretCode) {
    SecretCode = secretCode;
}}

在这里您定义了 RequestModel 但您没有将其传递给 api 调用。 Post 请求应该有正文。 因此在定义 api 调用时指定 @Body,如下所示。

@POST("QuestionsList")
Call<List<ModelObjects>> getQuestions(@Body RequestModel model);

然后在调用 getQuestion() 时传递模型。

RequestModel requestModel = new RequestModel();
requestModel.setSecretCode("341977082");
Call<List<ModelObjects>> call = api.getQuestions(requestModel);

更新: 如下更新您的 ModelObject

public class ModelObjects {
@SerializedName("Ques_No")
String Ques_No;
@SerializedName("Question")
String Ques;
@SerializedName("Answer")
String answer;
//same for other params as well
}

我能看到什么,您正在创建 RequestModel class 的对象,但您没有将它传递到任何地方。如果您想将 secretCode 与 post 网络调用一起发送,则必须将此 requestModel 实例传递给调用。

    public interface ApiInterface {
    String BASE_URL = "";
    @POST("QuestionsList")
    Call<List<ModelObjects>> getQuestions(@Body RequestModel requestModel);
    }

然后你可以调用这个方法,并且可以通过

传递这个requestModel对象
    Call<List<ModelObjects>> call = api.getQuestions(requestModel);

如果你想访问第一个对象,你可以通过

   List<ModelObjects> questionsList = response.body();
   ModelObject obj = questionList.get(0);
   String question = obj.getQues_No();

这个问题将成为第一个问题。