0

I have a website link that shows only a string like this 18.06#21.06#19.42 which continuously changes after some seconds. I tried to fetch it with Retrofit but could not fetch it like we need a Json but the link returns a String. Now, how can I access this data into my application in form of String?

website page link: https://prdec.com/status_app/status_app_return_string.php

Error:

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

ApiClient.java


public class ApiClient {
    public static Retrofit retrofit;
    public static String BASE_URL = "https://prdec.com/status_app/";

    public static Retrofit getRetrofit(){

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

**ApiService.java**

public interface ApiService {

@GET("status_app_return_string.php")
Call<String> getStringResponse();

}

inside **MainActivity.java**

private void getStatusResponse() {

    ApiService apiService;
    apiService = ApiClient.getRetrofit().create(ApiService.class);

    apiService.getStringResponse().enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Toast.makeText(MainActivity.this, response.body(), Toast.LENGTH_SHORT).show();
        }

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

            Log.d(TAG, "Failed: "+ t.getMessage());

            Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

2 Answers2

0

If you have got a string you can call split("#") on it and it will get you array of 3 strings with just your numbers. Double.valueOf() on them and you have doubles

Konstantin Pribluda
  • 12,042
  • 1
  • 26
  • 34
0

Have a look at Volley. You can make a StringRequest which simply receives a String response (Kotlin example below, but this link does have both Java and Kotlin depending what you are using).

// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "https://www.google.com"

// Request a string response from the provided URL.
val stringRequest = StringRequest(Request.Method.GET, url,
        Response.Listener<String> { response ->
            // TODO do something with the response
        },
        Response.ErrorListener {
            // TODO handle any error 
        }
    )

// Add the request to the RequestQueue.
queue.add(stringRequest)
David Kroukamp
  • 34,930
  • 13
  • 72
  • 130