-1

Interface: I enter the word to text and then click on the button and in the output the result

public interface Link {
    @FormUrlEncoded//аннотация
    @POST("https://translate.yandex.net/api/v1.5/tr.json/translate")
    Call<Object> translate(@FieldMap Map<String,String> map); 
}

enter image description here

How to run a retrofit in the background thread?

I enter the word to text and then click on the button and in the output the result,simple translator, I test this library

upward
  • 239
  • 1
  • 5
  • 14

2 Answers2

12

call.execute(); is a synchronous network call which should not be performed on the main thread. As they have pointed out to you, the best option you have is to use call.enqueue() like below:

call.enqueue(new Callback<Object>() {
   @Override
   public void onResponse(Call<Object> call, Response<Object> response) {
       response = response.body();
   }

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

  }

If the call is successful, you get your response in onResponse else in onFailure

  • i updated the topic,but not sure that my code correctly – upward Aug 08 '16 at 08:04
  • Line 10 from the picture you posted, 1) You don't need to assign a variable response to the call i.e `response = ` is not needed. Simply do `call.enqueue(...)` and get your response inside the `onResponse()` method. See the edited answer above – Hassan Semiu Ayomon Aug 08 '16 at 13:59
1

With com.squareup.retrofit2:retrofit:2.1.0

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call1, retrofit2.Response<ResponseBody> response) {


            BufferedReader reader = null;
            StringBuilder sb = new StringBuilder();

            reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            result = sb.toString();
          }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Tag","onResponse onFailure" );

        }
    });
ViramP
  • 1,659
  • 11
  • 11