Skip to main content
deleted 24 characters in body
Source Link
Daniel Nugent
  • 43.4k
  • 15
  • 114
  • 142

Not sure exactly what you're trying to do here, but theThe url in JsonObjectRequest() is not optional. The, and the JSONObject parameter is used to post parameters with the request to the url.

Using Http URL ConnectionHttpURLConnection:

 public class getData extends AsyncTask<String, String, String> {

        HttpURLConnection urlConnection;

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

            StringBuilder result = new StringBuilder();
            StringBuilder jsonStr = new StringBuilder();

            try {
                URL url = new URL("https://api.github.com/users/dmnugent80/repos");
                urlConnection = (HttpURLConnection) url.openConnection();
                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();
            }
            finally {
                urlConnection.disconnect();
            }


            return result.toString();
        }

        @Override
        protected void onPostExecute(String result) {

            //Do something with the JSON string

        }

    }
  

Not sure exactly what you're trying to do here, but the url is not optional. The JSONObject parameter is used to post parameters with the request to the url.

Using Http URL Connection:

 public class getData extends AsyncTask<String, String, String> {

        HttpURLConnection urlConnection;

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

            StringBuilder result = new StringBuilder();
            StringBuilder jsonStr = new StringBuilder();

            try {
                URL url = new URL("https://api.github.com/users/dmnugent80/repos");
                urlConnection = (HttpURLConnection) url.openConnection();
                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();
            }
            finally {
                urlConnection.disconnect();
            }


            return result.toString();
        }

        @Override
        protected void onPostExecute(String result) {

            //Do something with the JSON string

        }

    }
  

The url in JsonObjectRequest() is not optional, and the JSONObject parameter is used to post parameters with the request to the url.

Using HttpURLConnection:

 public class getData extends AsyncTask<String, String, String> {

        HttpURLConnection urlConnection;

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

            StringBuilder result = new StringBuilder();
           
            try {
                URL url = new URL("https://api.github.com/users/dmnugent80/repos");
                urlConnection = (HttpURLConnection) url.openConnection();
                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();
            }
            finally {
                urlConnection.disconnect();
            }


            return result.toString();
        }

        @Override
        protected void onPostExecute(String result) {

            //Do something with the JSON string

        }

    }
  
added 792 characters in body
Source Link
Daniel Nugent
  • 43.4k
  • 15
  • 114
  • 142
 public class getData extends AsyncTask<String, String, String> {

        HttpURLConnection urlConnection;

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

            StringBuilder result = new StringBuilder();
            StringBuilder jsonStr = new StringBuilder();

            try {
                URL url = new URL("http"https://wwwapi.androidgithub.com/"users/dmnugent80/repos");
   HttpURLConnection             urlConnection = (HttpURLConnection) url.openConnection();
   try {
            InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 

     String result          BufferedReader reader = readStreamnew BufferedReader(new InputStreamReader(in)); 

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

            }catch( Exception e) {
                e.printStackTrace();
            }
            finally {
                urlConnection.disconnect();
            }


            return result.toString();
        } 

        @Override
        protected void onPostExecute(String result) {

            //Do something with the JSON string

        }

    }
  

For more in-depth code and passing POST parameters, see this post: Sending a JSON HTTP POST request from Android

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     String result = readStream(in);
     JSONObject jObj = new JSONObject(result);
    finally {
     urlConnection.disconnect();
   }
 }

For more in-depth code and passing POST parameters, see this post: Sending a JSON HTTP POST request from Android

 public class getData extends AsyncTask<String, String, String> {

        HttpURLConnection urlConnection;

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

            StringBuilder result = new StringBuilder();
            StringBuilder jsonStr = new StringBuilder();

            try {
                URL url = new URL("https://api.github.com/users/dmnugent80/repos");
                urlConnection = (HttpURLConnection) url.openConnection();
                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();
            }
            finally {
                urlConnection.disconnect();
            }


            return result.toString();
        } 

        @Override
        protected void onPostExecute(String result) {

            //Do something with the JSON string

        }

    }
  
added 179 characters in body
Source Link
Daniel Nugent
  • 43.4k
  • 15
  • 114
  • 142

Not sure exactly what you're trying to do here, but the url is not optional. The JSONObject parameter is used to post parameters with the request to the url.

From the documentation: http://afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html

http://developer.android.com/training/volley/index.html

JsonObjectRequest

public JsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener) Creates a new request.

Parameters:

method - the HTTP method to use

url - URL to fetch the JSON from

jsonRequest - A JSONObject to post with the request. Null is allowed and indicates no parameters will be posted along with request.

listener - Listener to receive the JSON response

errorListener - Error listener, or null to ignore errors.

Using Http URL Connection:

http://developer.android.com/reference/java/net/HttpURLConnection.html

The code would be something like this:

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     String result = readStream(in);
     JSONObject jObj = new JSONObject(result);
    finally {
     urlConnection.disconnect();
   }
 }

For more in-depth code and passing POST parameters, see this post: Sending a JSON HTTP POST request from Android

Not sure exactly what you're trying to do here, but the url is not optional. The JSONObject parameter is used to post parameters with the request to the url.

From the documentation: http://afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html

http://developer.android.com/training/volley/index.html

JsonObjectRequest

public JsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener) Creates a new request.

Parameters:

method - the HTTP method to use

url - URL to fetch the JSON from

jsonRequest - A JSONObject to post with the request. Null is allowed and indicates no parameters will be posted along with request.

listener - Listener to receive the JSON response

errorListener - Error listener, or null to ignore errors.

Using Http URL Connection:

http://developer.android.com/reference/java/net/HttpURLConnection.html

The code would be something like this:

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     String result = readStream(in);
     JSONObject jObj = new JSONObject(result);
    finally {
     urlConnection.disconnect();
   }
 }

Not sure exactly what you're trying to do here, but the url is not optional. The JSONObject parameter is used to post parameters with the request to the url.

From the documentation: http://afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html

http://developer.android.com/training/volley/index.html

JsonObjectRequest

public JsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener) Creates a new request.

Parameters:

method - the HTTP method to use

url - URL to fetch the JSON from

jsonRequest - A JSONObject to post with the request. Null is allowed and indicates no parameters will be posted along with request.

listener - Listener to receive the JSON response

errorListener - Error listener, or null to ignore errors.

Using Http URL Connection:

http://developer.android.com/reference/java/net/HttpURLConnection.html

The code would be something like this:

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     String result = readStream(in);
     JSONObject jObj = new JSONObject(result);
    finally {
     urlConnection.disconnect();
   }
 }

For more in-depth code and passing POST parameters, see this post: Sending a JSON HTTP POST request from Android

added 584 characters in body
Source Link
Daniel Nugent
  • 43.4k
  • 15
  • 114
  • 142
Loading
Source Link
Daniel Nugent
  • 43.4k
  • 15
  • 114
  • 142
Loading