android studio中的API同步连接无法正常工作,但异步工作

时间:2018-06-05 17:22:54

标签: java android api retrofit

我有一个api,它在异步模式下工作但在同步模式下不起作用。 我有一个ApiUtils课程:

public class RetrofitClient {
    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {

        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

并拥有RetrofitClient类:

public class Post {
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("body")
    @Expose
    private String body;
    @SerializedName("userId")
    @Expose
    private Integer userId;
    @SerializedName("id")
    @Expose
    private Integer id;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Post{" +
                "title='" + title + '\'' +
                ", body='" + body + '\'' +
                ", userId=" + userId +
                ", id=" + id +
                '}';
    }
}

My Post ViewModel类是:

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.google.code.gson:gson:2.8.4'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

我的gradle依赖项是:

//Not work
    private void SyncCall() {
         Call<Post> ins= mAPIServices.savePost("test title", "test body", 12);
        try {
            String s =ins.execute().body().toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Worked
    private void AsyncCall() {
        mAPIServices.savePost("test title", "test body", 12)
                .enqueue(new Callback<Post>() {
                    @Override
                    public void onResponse(Call<Post> call, Response<Post> response) {
                        String s = response.body().toString();
                    }

                    @Override
                    public void onFailure(Call<Post> call, Throwable t) {
                    }
                });
    }

我在Android清单中授予了互联网权限。 我为该api编写了2个用于异步调用和同步调用的函数。但问题是我的Async方法工作但我的Sync方法不起作用:

groupby

1 个答案:

答案 0 :(得分:0)

我可以找到我的解决方案。 我们必须创建一个像这样的新类:

public class InnerClass extends AsyncTask {
    @Override
    protected Post doInBackground(Object[] objects) {
        APIServices mAPIServices = ApiUtils.getAPIService();
        Call<Post> ins= mAPIServices.savePost("test title", "test body", 12);
        Post ret=new Post();
        try {
            ret =ins.execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
}

然后我们可以用这些代码调用api:

AsyncTask s=new InnerClass().execute();
        try {
            Post a=(Post)s.get();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }