Answer the question
In order to leave comments, you need to log in
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");
}
}
});
}
}
public interface API
{
@GET("/get")
Response getTranslate(@Query("q") String word, @Query("langpair") String perevod);
}
http://api.mymemory.translated.net/get?q={ЗНАЧЕНИЕ1}&langpair={ЗНАЧЕНИЕ2}
по этой ссылке идет возрат json
http://api.mymemory.translated.net/get?q=Hello!&langpair=en|ru
Answer the question
In order to leave comments, you need to log in
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question