Android 改造 Post 数据

Android Retrofit Post Data

我正在使用带有 Retrofit 的 Android Recyclerview,它在没有任何 Post 数据的情况下工作。我需要 post 我当前代码中的一些数据。

下面是我当前的 ApiInterface

public interface ApiInterface {
   @GET("mypage.php")
   Call<Pojo> getData();
}

在我的 activity 中,我通过以下代码调用它

ApiInterface apiInterface = (ApiInterface) RetrofitClient.getRetrofitInstance().create(ApiInterface.class);
    Call<Pojo> listingdata = apiInterface.getData();
    listingdata.enqueue(new Callback<Pojo>() {
        @Override
        public void onResponse(Call<Pojo> call, Response<Pojo> response) {

            if(response.isSuccessful()){
                recycleradpter recycleradpter = new recycleradpter(response.body().getData());
                recyclerView.setAdapter(recycleradpter);
                progressbar2.setVisibility(View.GONE);
            }

        }

        @Override
        public void onFailure(Call<Pojo> call, Throwable t) {
            //System.out.println("12345678934567890234567890");
            //Toast.makeText(getActivity(), "No Connected internet", Toast.LENGTH_SHORT).show();
           progressbar2.setVisibility(View.GONE);
            dialogfunction();
        }
    });

如何根据上面代码中传递的数据获取数据

如果您想发送 post 请求,则必须创建如下所示的方法。该方法的调用将类似于get请求。只需传递 post 正文的参数即可。详情可以here.

 @POST("mypage.php")
 Call<Pojo> postData(
     @Field("param1") String param1,
     @Field("param2") int param2
 );
@FormUrlEncoded
@POST("your_php_file.php")
Call<ResponseBody> getLanguageCall(@Field("lang_code") String lang_code, @Field("app_id") String app_id);


private void getLanguageCall() {
    progressDialog.setVisibility(VISIBLE);
    Call<ResponseBody> call = ApiClient.getClient().create(ApiInterface.class).getLanguageCall(PreferenceManager.getStringPreference(SplashActivity.this, appLanguage), PreferenceManager.getStringPreference(SplashActivity.this, PreferenceManager.APP_ID));
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            progressDialog.setVisibility(View.GONE);
            if (!response.isSuccessful()) {
                showToast(response.code() + " : " + response.message());
                return;
            }
            try {
                JSONObject root = new JSONObject(response.body().string());
                HashMap<String, String> list = new HashMap<String, String>();
                JSONArray keyData = root.getJSONArray("key_data");
                for (int i = 0; i < keyData.length(); i++) {
                    JSONObject obj = keyData.getJSONObject(i);
                    list.put(obj.getString("Key_Name").toLowerCase(), obj.getString("Key_Value"));
                }
                finish();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
            progressDialog.setVisibility(View.GONE);
            showToast("Error ! Server Error.");
        }
    });
}