我在进行改造时遇到问题

时间:2019-06-03 10:25:01

标签: android retrofit

我在进行改造时遇到问题。 我的回应不成功,我也不知道为什么。 我将在这里发布我的json数组,我的主要活动名为Education_Classes, 而且我也会发布我的课程。

{
  "classes": [
    {
      "class_id": "1",
      "class_name": "class1",
      "class_description": "des1"
    },
    {
      "class_id": "2",
      "class_name": "class2",
      "class_description": "des2"
    },
    {
      "class_id": "3",
      "class_name": "class3",
      "class_description": "des3"
    }
  ],
  "status": "1"
}

主要课程

public class Education_Classes extends AppCompatActivity {


    private List < Classes > classesList = new ArrayList <>();
    private RecyclerView recyclerView;
    private ClassesAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_education__classes );

        recyclerView = (RecyclerView) findViewById( R.id.recyclerClasses );

        Retrofit.Builder builder = new Retrofit.Builder().baseUrl( "http://bimehsan.ir/api-v1/" ).addConverterFactory( GsonConverterFactory.create() );
        Retrofit retrofit = builder.build();
        final WebService webService = retrofit.create( WebService.class );


        SharedPreferences shareToken = getSharedPreferences( "userdetails", MODE_PRIVATE );


        String getToken = shareToken.getString( "token", "no name" );

        String tokeyWhittokenType = "Bearer" + " " + getToken;


        Call < ClassesResponse > call = webService.getClasses( tokeyWhittokenType );

        call.enqueue( new Callback < ClassesResponse >() {

            @Override
            public void onResponse(Call < ClassesResponse > call, Response < ClassesResponse > response) {


                if ( response.isSuccessful() ) {

                    Toast.makeText( Education_Classes.this, "Sucsess", Toast.LENGTH_SHORT ).show();

                    classesList = response.body().getClasses();
                    mAdapter = new ClassesAdapter( classesList );
                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager( getApplicationContext() );
                    recyclerView.setLayoutManager( mLayoutManager );
                    recyclerView.setItemAnimator( new DefaultItemAnimator() );
                    recyclerView.setAdapter( mAdapter );

                } else {

                    Toast.makeText( Education_Classes.this, "خطا در گرفتن اطلاعات", Toast.LENGTH_SHORT ).show();


                }


            }

            @Override
            public void onFailure(Call < ClassesResponse > call, Throwable t) {

                Toast.makeText( Education_Classes.this, "اتصال خود را به اینتریت بررسی نمایید.", Toast.LENGTH_SHORT ).show();

            }


        } );

    }
}

ClassesResponse

public class ClassesResponse {

    private List<Classes> classes;
    private String status;


    public ClassesResponse(List<Classes> classes, String status) {
        this.classes = classes;
        this.status = status;
    }

    public List<Classes> getClasses() {
        return classes;
    }

    public void setClasses(List<Classes> classes) {
        this.classes = classes;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}


public class Classes {

    private String class_id;
    private String class_name;
    private String class_description;


    public Classes(String class_id, String class_name, String class_description) {
        this.class_id = class_id;
        this.class_name = class_name;
        this.class_description = class_description;
    }

    public String getClass_id() {
        return class_id;
    }

    public void setClass_id(String class_id) {
        this.class_id = class_id;
    }

    public String getClass_name() {
        return class_name;
    }

    public void setClass_name(String class_name) {
        this.class_name = class_name;
    }

    public String getClass_description() {
        return class_description;
    }

    public void setClass_description(String class_description) {
        this.class_description = class_description;
    }
}

和我的 webService

public interface WebService {

    @POST("login")
    @FormUrlEncoded
    Call<User> login(@Field("national_code") String national_code, @Field("password") String password);


    @GET("all-classes")
    Call<ClassesResponse> getClasses(@Header("Authorization") String tokeyWhittokenType);

}

我希望我的response.isSuccessful()应该是真实的,但是它是错误的!

3 个答案:

答案 0 :(得分:2)

您的问题是打电话给Api

所有类别的Api是 POST 请求,但是您发送 GET 请求:

更改:

Select

收件人:

@GET("all-classes")

答案 1 :(得分:0)

您没有对响应变量使用@SerializedName("response_key")注释:

public class ClassesResponse {

    @SerializedName("classes")
    private List<Classes> classes;
    @SerializedName("status")
    private String status;


    public ClassesResponse(List<Classes> classes, String status) {
        this.classes = classes;
        this.status = status;
    }

    public List<Classes> getClasses() {
        return classes;
    }

    public void setClasses(List<Classes> classes) {
        this.classes = classes;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}


public class Classes {

    @SerializedName("class_id")
    private String class_id;
    @SerializedName("class_name")
    private String class_name;
    @SerializedName("class_description")
    private String class_description;


    public Classes(String class_id, String class_name, String class_description) {
        this.class_id = class_id;
        this.class_name = class_name;
        this.class_description = class_description;
    }

    public String getClass_id() {
        return class_id;
    }

    public void setClass_id(String class_id) {
        this.class_id = class_id;
    }

    public String getClass_name() {
        return class_name;
    }

    public void setClass_name(String class_name) {
        this.class_name = class_name;
    }

    public String getClass_description() {
        return class_description;
    }

    public void setClass_description(String class_description) {
        this.class_description = class_description;
    }
}

另外,我确实在Postman上进行了测试。这是POST请求,而不是GET,因此您需要将@GET("all-classes")更改为@POST("all_classes")

答案 2 :(得分:0)

我有同样的问题。该请求返回一个空对象。但是android代码是正确的。问题出在服务器代码中。检查服务器代码。可能会有帮助