Search code examples
androidjsondjangohttpclientandroid-ion

Why does my URI return the correct JSON in my browser, but not in from my httpClient?


I'm relatively new to Django and RESTful APIs.

I'm writing an Android Application that executes a GET request to my API, which I've set up using Django and Postgresql. I am executing this request asynchronously using the Ion library. I dont think Ion is a problem, because this code works when I try to GET from sample URIs like http://ip.jsontest.com/. My code looks like this.

    public class ConnectToBackend {

    //Contains public static functions

    public static void getAllQuestions(final Context mcontext){


        Ion.with(mcontext)
        .load("bhive.herokuapp.com/api/questions")
        .asJsonObject()
        .setCallback(new FutureCallback<JsonObject>() {
           @Override
            public void onCompleted(Exception e, JsonObject result) {
               // this is called back onto the ui thread, no Activity.runOnUiThread or Handler.post necessary.

               Log.d("ConnectToBackend", "getAllQuestions called");
               if (e != null) {
                   Toast.makeText(mcontext, "Error loading questions " + e.toString(), Toast.LENGTH_LONG).show();
                   Log.d("ConnectToBackend", e.toString());
                   return;
               }
               else{
                   Log.d("connectToBackend", "Received JSON array for getAllQuestions");
                   Log.d("connectToBackend", "jsonObject is " + result.toString());
                   return;
 }
            }
        });
        return;
    }}

As you can see, the URI is bhive.herokuapp.com/api/questions. When I try this in my browser, it returns the JSON string that I expect. However, when I try to get with this code, I expect the same JSON string but instead I get the exception:

java.lang.Exception: Invalid URI

Why is it that my browser returns the expected JSON but when I try this URI like this, it doesn't work? How is my URI different than from a sample URI like http://ip.jsontest.com/, which returns the same thing on my browser? Perhaps I have a conceptual shortfall somewhere.


Solution

  • Your URI is missing a scheme. Try using http://bhive.herokuapp.com/api/questions instead. The http is important.