charbel karam
charbel karam

Reputation: 11

Cannot fetch JSON using retrofit in Android

I have set up a XAMPP server on a virtual machine, and I've set up a PHP code to return a very simple JSON object, the code is in android_connect/get_all_products

<?php

// array for JSON response
$response = array();
$response["name"] = "IPhone";

echo json_encode($response);
?>

And the code works, when I enter in my browser http://192.168.2.107/android_connect/get_all_products.php I get the correct JSON, even on an external machine on my network.

But I am unable to fetch it in an android application using Retrofit2.

below is a POJO I created to represent the JSON:

public class Results {

    @SerializedName("name")
    private String name;

    public Results(String pid, String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }
}

This is the API interface:

public interface Api {

    String BASE_URL = "http://192.168.2.107/";
    @GET("android_connect/get_all_products.php")
    Call<Results> displayName();
}

And this is the retrofit client instance:

public class RetrofitClient {

    private static RetrofitClient instance = null;
    private Api myApi;

    private RetrofitClient() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        myApi = retrofit.create(Api.class);
    }

    public static synchronized RetrofitClient getInstance() {
        if (instance == null) {
            instance = new RetrofitClient();
        }
        return instance;
    }

    public Api getMyApi() {
        return myApi;
    }
}

and finaly this is the main activity code:

public class MainActivity extends AppCompatActivity {

    ListView superListView;

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

        superListView = findViewById(R.id.superListView);

        getSuperHeroes();
    }

    private void getSuperHeroes() {
        Call<Results> call = RetrofitClient.getInstance().getMyApi().displayName();
        call.enqueue(new Callback<Results>() {
            @Override
            public void onResponse(Call<Results> call, Response<Results> response) {
                Toast.makeText(getApplicationContext(), response.body().getName(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<Results> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "An error has occured", Toast.LENGTH_LONG).show();
            }

        });
    }
}

Everytime I get a toast saying An error has occured (as it means the call has failed somewhere)

NOTES: I have added INTERNET permission in the manifest.xml file I have tried some variation of my code on an external server and it runs fine, seems like the problem happens when I try to connect to my server.

Thank you in advance, I am very new to android programing and I have an assignment to connect it to a php server.

EDIT: as ADyson pointed, the throwable t in onFailure, gives the folowing message: cLEARTEXT communication to 192.168.2.107 not permitted by network security policy. What does that mean? and what should I do?

Upvotes: 0

Views: 103

Answers (1)

charbel karam
charbel karam

Reputation: 11

Resolved: android network security blocks HTTP request by default, to enable it add to Manifest.xml:

android:usesCleartextTraffic="true"

Upvotes: 1

Related Questions