WISHY
WISHY

Reputation: 11999

NullPointerException in converting retrofit response to string?

This is my retrofit class

public class ApiClient {
public static final String BASE_URL = AppConstants.BASE_URL;
private static Retrofit retrofit = null;


public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

public interface RegisterAPI {
    @POST(AppConstants.LOGIN_PAGE)
    Call<JSONObject> getLogin(@Body JSONObject loginParams);
}
}

This is the retrofit request

JSONObject loginParams = ((MainActivity) getActivity()).requestParams.getLoginParams(etUserName.getText().toString(), etPassword.getText().toString());
            ApiClient.RegisterAPI apiService =
                    ApiClient.getClient().create(ApiClient.RegisterAPI.class);


            Call<JSONObject> call = apiService.getLogin(loginParams);
            call.enqueue(new Callback<JSONObject>() {
                @Override
                public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                    Log.e("response", response.body().toString());
                }

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

                }
            });

While converting the response to string I get null pointer

java.lang.NullPointerException
at com.vfirst.ifbagro.login.LoginFragment$1.onResponse(LoginFragment.java:96)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:730)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:5214)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
 at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1532

Answers (2)

Dev Tamil
Dev Tamil

Reputation: 649

Try this

BufferedReader reader = null;
String output = "";

    try {
        reader = new BufferedReader(new InputStreamReader(response.getBody().in()));
        output = reader.readLine();
    } catch (IOException e) {
            e.printStackTrace();
    }
    Toast.makeText(getActivity(), output, Toast.LENGTH_LONG).show();

Upvotes: 0

Niko
Niko

Reputation: 8153

Try logging just response.body() to first see if its not null.

If you are using OkHttpClient you can add interceptor and log the response, this is guaranteed to work:

public Response intercept(Chain chain) throws IOException {
    Request.Builder builder = chain.request().newBuilder();
    // Add custom headers etc with builder
    ....
    Request request = builder.build();
    Response response = chain.proceed(request);
    String body = response.body().string()
    ....

Upvotes: 1

Related Questions