1

I am trying to fetch JSON from a REST API as a string.

I am using retrofit with scalarConverter for that purpose. I am able to pass the URL to fetch and my retrofit instance is also created successfully but I am not getting any response from the server. PS: there is no request on the server, so that means my request is not going out from my machine.

I am new to android, kindly help me.

Retrofit instance creation:

Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(base)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
jsonApi jsonapi=retrofit.create(jsonApi.class);
Call<String> stringcall=jsonapi.getStringResponse(speech);

jsonApi interface:

public interface jsonApi {
    @GET
    Call<String> getStringResponse(@Url String url);
}

base: it is the base URL

speech: it is a variable containing rest of the URL to be processed.

when I run, the app gets stuck here with this message being displayed in Run Tab:

W/OpenGLRenderer: Fail to change FontRenderer cache size, it already initialized
W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36

1 Answers1

1

With the below line

Call<String> stringcall=jsonapi.getStringResponse(speech);

you just get a Call object which is a representation of the HTTP request, and just by that, the request isn't executed. You need to use this object and call the execute method to make a synchronous request or the enqueue method to make an asynchronous request.

So in case you want to make a sync request, try the below:

Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(base)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
jsonApi jsonapi=retrofit.create(jsonApi.class);
Call<String> stringcall=jsonapi.getStringResponse(speech);
try {
    Response<String> response = stringcall.execute();
    String result = response.body();
} catch (Exception ex) { 
    //handle exception
}

The documentation of Call interface is here, for your reference.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • I tried to make a sync request by `execute()` method but the app crashed. So, i tried the `enqueue` method in which the app is not crashing but it showing nothing. @Madhu Here is the snippet: `stringcall.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { //do something Log.d("here","here"); } @Override public void onFailure(Call call, Throwable t) {} });` – Syed Asnal Rizvi May 28 '19 at 13:48
  • @SyedAsnalRizvi what is the error/exception when the app crashed on using the `execute` method? – Madhu Bhat May 28 '19 at 13:56
  • W/System.err: **android.os.NetworkOnMainThreadException** W/System.err: at **android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)** @Madhu Bhat – Syed Asnal Rizvi May 28 '19 at 14:26
  • @SyedAsnalRizvi looks like its due to making the network call over UI thread. Can you try the answer in https://stackoverflow.com/questions/13270372/android-os-networkonmainthreadexception-when-trying-to-connect-over-wifi-using which suggests using AsyncTask for the network calls. – Madhu Bhat May 28 '19 at 14:34
  • I got it, thanks for the help. Much appreciated. @Madhu Bhat – Syed Asnal Rizvi May 28 '19 at 21:21