som3_l0cust
som3_l0cust

Reputation: 307

I can't get JSON objects from a REST API URL

So I have been trying to retrieve JSON files which are basically posts from my database. They can be fetched from the Django REST API that I am running. I can access the url and see the available posts from any devices within my network. However, I can't retrieve them so that they can be posted on my Android app.

My Log in the postExecute of my AsyncTask doesn't show any JSON objects and is just empty.

It doesn't return any error messages in my logcat so I am not sure what is the problem.

I am trying to make a prototype social networking type stuff where the posts are stored in a database inside my database and fetch them through the API.

I am using AsyncTask to connect to my URL and do processing there

Posts.java

public class Posts extends AppCompatActivity {

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

            PostsDetails postDetailsHelper = new PostsDetails();

            postsDoneBtn = (Button) findViewById(R.id.PostsDoneButton);

            postDetailsHelper.callPostDetails("192.168.0.18:8000/api/");

            postDetailsHelper.postDetailsCalled('n');

            postsDoneBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Posts.this, MainActivity.class));

                }
            });
        }

        public class PostsDetails {

            // calls the execute for AsyncTask
            private void callPostDetails(String theurl){
                PostsHelper = new WSAdapter.SendPostsRequest();
                // sets if post details are called
                postDetailsCalled('y');
                // executes AsyncTask
                PostsHelper.execute(theurl);
            }

        }

}

WSAdapter.java

public class WSAdapter extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    static public class SendPostsRequest extends AsyncTask<String, String, String> {

        // Add a pre-execute thing
        HttpURLConnection urlConnection;

        @Override
        protected String doInBackground(String... params) {

            StringBuilder result = new StringBuilder();

            try {
                urlConnection = (HttpURLConnection) new URL(params[0]).openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

            }catch( Exception e) {
                e.printStackTrace();
            }

            return result.toString();
        }

        @Override
        protected void onPostExecute(String result) {

            // expecting a response code fro my server upon receiving the POST data
            Log.e("TAG", result);

    }
}

Upvotes: 0

Views: 65

Answers (1)

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10871

You need to add the protocol scheme to your URL: http://, https://, or any other you need.
So it should look like this:

postDetailsHelper.callPostDetails("http://192.168.0.18:8000/api/");

Upvotes: 1

Related Questions