简体   繁体   English

Retrofit Android从REST API获取日期无法正常工作

[英]Retrofit Android get date from REST API not working properly

I have a rest API on spring. 我在春天有一个休息API。 I can get data from url: localhost:8080/all I created class: 我可以从url获取数据:localhost:8080 /我创建的所有类:

public class DataAPI {
    private static final String CLASS_TAG = "DataAPI";
    RestAdapter retrofit;
    MyWebService myWebService;
    private List<Teacher> list;


    public DataAPI() {
        String url = "http://10.101.12.31:8080/";
        retrofit = new RestAdapter.Builder()
                .setEndpoint(url)
                .build();
        myWebService = retrofit.create(MyWebService.class);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public List<Department> getAllDepartments() {
        try {
            myWebService.getAllTeacher(new Callback<List<Teacher>>() {

                @Override
                public void success(List<Teacher> data, Response response) {
                    Log.d(CLASS_TAG, data.toString());
//                  data.stream().forEach(System.out::println);
                    list= data;

                }

                @Override
                public void failure(RetrofitError error) {

                }
            });

        } catch (Exception e) {
            Log.d(CLASS_TAG, e.toString());
        }

        return this.departmentList;
    }

MyWebService: 为MyWebService:

public interface MyWebService {
    @GET("/all")
    void getAllDepartments(Callback<List<Teacher>> pResponse);

MainActivity: 主要活动:

public class MainActivity extends AppCompatActivity {
    private static final String CLASS_TAG = "MainActivity";

    TextView textView;
    DataAPI dataAPI;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataAPI = new DataAPI();
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);


        findViewById(R.id.button_get).setOnClickListener(new View.OnClickListener() {


            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                textView.setText("");


                List<Teacher> list = dataAPI.getAllDepartments();
                System.out.println(list == null);
                if (list != null){
                    list.stream().forEach(x -> textView.setText(textView.getText() + "\n" + x.toString()));
                }
            }
        });

I have a button on my application. 我的应用程序上有一个按钮。 When I first time click on it, nothing change, but when i click second time, data are loading. 当我第一次点击它时,没有任何变化,但是当我第二次点击时,数据正在加载。 When I paste all code form DataAPI to method onClick,data are loading at first time. 当我将所有代码格式DataAPI粘贴到方法onClick时,数据将在第一时间加载。

use interface to get data to called activity like below, first write interface, 使用接口将数据传递给被调用的活动,如下所示,首先写入接口,

public class APICallBack {

public interface GetResponseCallback{
    void onSuccess(yourresponsetype response);
    void onFailed();
}
}

implement this interface in your MainActivity . 在MainActivity中实现此接口。 when you call dataAPI.getAllDepartments(); 当你调用dataAPI.getAllDepartments();

pass the interface to your DataAPI class. 将接口传递给DataAPI类。

 public List<Department> getAllDepartments(final APICallback.GetResponseCallback callback){

            @Override
            public void success(List<Teacher> data, Response response) {
                Log.d(CLASS_TAG, data.toString());
//                  data.stream().forEach(System.out::println);
                list= data;
              callback.onSuccess(response);
            }

}

If one can use that RestAdapter.Builder with GSON , one can define a date-format for the parser: 如果可以将RestAdapter.BuilderRestAdapter.Builder一起使用, RestAdapter.Builder GSON解析器定义日期格式:

Gson gson = new GsonBuilder()
    .setDateFormat(API_DATE_FORMAT)
    .create();

Retrofit retrofit = new retrofit2.Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create(gson))
    .baseUrl(API_BASE_URL)
    .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM