没有使用Retrofit

时间:2017-04-06 18:54:56

标签: java android rest api retrofit

我得到了一个N​​ullpointer例外 哪里出错了? 这里有完整的错误信息:

  java.lang.NullPointerException:尝试调用接口方法' int java.util.List.size()'在null对象引用上                                                                                    在com.babul.resttest.MainActivity $ 1.onResponse(MainActivity.java:81)                                                                                    at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:135)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5221)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在java.lang.reflect.Method.invoke(Method.java:372)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et_name) EditText etName;
    @BindView(R.id.tv_details) TextView tvDetails;
    @BindView(R.id.btn_get_data) Button btnGetData;
    @BindView(R.id.btn_insert_data) Button btnInsertData;

    private ProgressDialog progressDialog;
    private static final String BASE_URL = "http://www.json-generator.com/";
    private static final String USER = "cqMPOrusMO?indent=2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setCancelable(false);
    }

    @OnClick({R.id.btn_get_data, R.id.btn_insert_data})
    public void setBtnOnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_get_data:
                getStudent();
                break;
            case R.id.btn_insert_data:
                break;
        }
    }

    private void getStudent() {
        showProgressDialog();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService service = retrofit.create(ApiService.class);
        Call<List<Student>> call = service.getStudent(USER);
        call.enqueue(new Callback<List<Student>>() {
            @Override
            public void onResponse(Call<List<Student>> call, Response<List<Student>> response) {
                List<Student> students = response.body();

                String details = "";
                String name;
                String address;
                String mobile;

                for (int i = 0; i < students.size(); i++) { // Error Here
                    name = students.get(i).getName();
                    address = students.get(i).getAddress();
                    mobile = String.valueOf(students.get(i).getMobile());

                    details += i + ":" + name + " " + address + " " + mobile + "\n";
                }

                tvDetails.setText(details);
                hideProgressDialog();
            }

            @Override
            public void onFailure(Call<List<Student>> call, Throwable t) {
                Log.d("Error", t.getMessage());
            }
        });
    }

    private void showProgressDialog() {
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    private void hideProgressDialog() {
        if (progressDialog.isShowing()) {
            progressDialog.hide();
        }
    }
}

接口:

   public interface ApiService {

        @GET("api/json/get/{user}")
        Call<List<Student>> getStudent(@Path("user") String user);
    }

型号:

@SuppressWarnings("unused")
public class Student {

    @SerializedName("address")
    private String address;
    @SerializedName("mobile")
    private Long mobile;
    @SerializedName("name")
    private String name;
    @SerializedName("students")
    private List<Student> students;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Long getMobile() {
        return mobile;
    }

    public void setMobile(Long mobile) {
        this.mobile = mobile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

1 个答案:

答案 0 :(得分:1)

检查您的回复是否成功:

   if (response.isSuccessful()) {
           List<Student> students = response.body();

            String details = "";
            String name;
            String address;
            String mobile;
                    for (int i = 0; i < students.size(); i++) {
                    name = students.get(i).getName();
                    address = students.get(i).getAddress();
                    mobile = String.valueOf(students.get(i).getMobile());

                    details += i + ":" + name + " " + address + " " + mobile + "\n";
                }

                tvDetails.setText(details);
                hideProgressDialog();
                } else {
                     Log.d("Error", t.getMessage());
                }