N
N
newdancer2016-01-10 18:32:55
Java
newdancer, 2016-01-10 18:32:55

How to parse data using the Retrofit library?

How to parse data using the Retrofit library?
I tried to implement it this way:

public class MainActivity extends AppCompatActivity
{
    final String TAG = "myLogs";

    EditText editVvodText;

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

        // Создаем интерфейс для работы с API
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://api.mymemory.translated.net")
                .build();
        final API service = restAdapter.create(API.class);

        editVvodText = (EditText) findViewById(R.id.editVvodText);
        editVvodText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if(editVvodText.getText().length()>5)
                {
                    service.getTranslate(editVvodText.getText().toString(), "en|ru");
                }

            }
        });
    }
}

The request interface looks like this:
public interface API
{
    @GET("/get")
    Response getTranslate(@Query("q") String word, @Query("langpair") String perevod);
}

The request must be made using the following link:
http://api.mymemory.translated.net/get?q={ЗНАЧЕНИЕ1}&langpair={ЗНАЧЕНИЕ2}
по этой ссылке идет возрат json 
http://api.mymemory.translated.net/get?q=Hello!&langpair=en|ru

What am I doing wrong? prompt an idea?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aol-nnov, 2016-01-10
@aol-nnov

clue is simple

V
Viktor Potapov, 2016-01-11
@midikko

Most likely some problems in getting a response. Check that the server returns a valid json to you, and also enable retrofit debugging. enabled by adding a parameter during initialization.
it's done like this

final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(15, TimeUnit.SECONDS);
        RestAdapter retrofit = new RestAdapter.Builder()
                .setEndpoint("http://localhost:5486")
                .setClient(new OkClient(okHttpClient))
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();
        RFIDService service = retrofit.create(RFIDService.class);

after that you will see both the request and the response, and everything should fall into place.
PS Nobody forbids catching and logging try-catch'em as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question