skip to Main Content

I made a REST api using PHP, and then I hosted it in hostinger you can visit the api
here(It just contains a json data). In my browser I am able to get the json data. But in android studio, the response returns an empty string.

This is the content of the api

{"FurnitureID":"4","Name":"Caesar Chair","Description":"It is part of the Fiber chair family. The armchair has a smooth, curved shell covered in a bio-composite material which includes 25% wood fibers. The other side of chairs, one with a tube base and one with a wood base. It is both simple and elegant, with gentle curves and a lightweight appearance. Any of these would make great contemporary dining chairs for minimalist and classy settings.","AR_Object":"modern.glb","Image":"modern_chair.jpg","Price":"2,500.00","Category":"chair","isFavorite":false}

This is what the logcat is saying

2021-12-04 12:25:01.714 1273-1448/com.example.ar_furniture D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2021-12-04 12:25:01.718 1273-1448/com.example.ar_furniture I/DpmTcmClient: RegisterTcmMonitor from: $Proxy0
2021-12-04 12:25:02.443 1273-1448/com.example.ar_furniture E/Volley: [10014] NetworkUtility.shouldRetryException: Unexpected response code 503 for https://ar-furniture-cf.preview-domain.com/ar-furniture-server/furnitures/getFurnitureDetails/4/1
2021-12-04 12:25:04.376 1273-1419/com.example.ar_furniture V/FA: Inactivity, disconnecting from the service

This is my android studio code

Volley.newRequestQueue(this).add(new StringRequest(
            Request.Method.GET,
            "https://ar-furniture-cf.preview-domain.com/ar-furniture-server/furnitures/getFurnitureDetails/4/1",
            res -> {
                Toast.makeText(this, res, Toast.LENGTH_LONG).show();
            },
            error -> {
                Toast.makeText(this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        ));

My code in php is just a plain data fetch using PDO

I dont know if the problem is in the hostinger. Because I tried hosting it in 000webhost for free then it worked in my android. Then I paid a single hosting plan in hostinger becaused I need it in school.

By the way I use mysql for database

2

Answers


  1. Chosen as BEST ANSWER

    Jerson is right. The problem is the cloudflare doing a verification in my website before entering the actual website. I fixed the problem by using a registered domain. because I thought that the domain that I was using is registered.


  2. As the response is a JSON object, you have to convert it like

     try{
          JSONObject obj = new JSONObject(response);
        } 
    catch (JSONException | NumberFormatException e) {
          e.printStackTrace();
        }
    

    Then get the result by key like

    Log.d("FurnitureID: ", obj.getString("FurnitureID"));
    Log.d("Name: ", obj.getString("Name"));
    

    Here is a link of complete example. You can check that too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search