0

I have an api that it workes in Asynchronous mode but not work in Synchronous mode. I have an ApiUtils class :

public class ApiUtils {
    private ApiUtils() {}

    public static final String BASE_URL = "http://jsonplaceholder.typicode.com/";
    public static APIServices getAPIService() {

        return RetrofitClient.getClient(BASE_URL).create(APIServices.class);
    }
}

And have RetrofitClient class :

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;
    }
}

My Post ViewModel class is :

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 gradle dependencies is :

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'

And i granted internet permission in android manifest . And i wrote 2 functions for Async calling and Sync calling for that api . But the problem is my Async method workes but my Sync method not workes :

//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) {
                    }
                });
    }
  • What is the error? What do you mean when you say your synchronous method is not working? Also, why not just use the async method? – Adam Jun 05 '18 at 18:20
  • I want to catch the return value from my api method to pass it to upper layer (from presenter to view). I want to run it synchronously. When i insert a breakpoint on line 'String s =ins.execute().body().toString();' , my program closed and even catch block not fired – Amir Hossein Khajegan Jun 05 '18 at 18:23
  • You can use await or postback to catch the return value from an async method. Separate the line to see better what is going on. String s = ins.execute(); *NEW LINE* String y = s.body(); *NEW LINE* String z = y.toString();....Then step through each line and evaluate each time the object changes. – Adam Jun 05 '18 at 18:26
  • I did that ,but in line ins.execute(), the program crashed , please help me and create a project like me if you can , thank you a lot – Amir Hossein Khajegan Jun 05 '18 at 18:30
  • I am not going to be able to recreate your service. You need to figure out why that line is crashing via stacktrace/debug message. Maybe it is down? Maybe you are passing invalid data? – Adam Jun 05 '18 at 18:32
  • My async method with same input data worked well but non async method not work , i followed a lot of sample in youtube but not work for me , now i copy that error in debug window in end of my question – Amir Hossein Khajegan Jun 05 '18 at 18:35
  • If your 'savePost' call is working, why are you calling execute? The 'working' async method does not call that. You may need to call ins.enque() however. – Adam Jun 05 '18 at 18:37
  • Because i need return value immediately, with enqueue calling api will postpone and i dont want that , i want to return the return value immediately – Amir Hossein Khajegan Jun 05 '18 at 19:01
  • 4
    Possible duplicate of [How do I fix android.os.NetworkOnMainThreadException?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) – Dalija Prasnikar Jun 05 '18 at 19:07
  • Dalija Prasnikar, My question is very different , please read this completely. Thank you – Amir Hossein Khajegan Jun 05 '18 at 21:02

1 Answers1

0

I can find my solution . We have to create a new class like this :

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;
    }
}

And then we can call the api synchronous with these code :

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