Search code examples
javaandroidjsonget

How can i send the request and receive the response (JSON ) via JAVA (android)?


How can I send in java(android) the http request and parse response in JSON format. I know that this is simple but i'm newbie in Java (android).


Solution

  • It seems that you would want to send a simple http get request to the provided url. To send a simple GET request:

    1. Create an object of HttpClient

    HttpClient client = new DefaultHttpClient();

    1. Create an object of HttpGet

    HttpGet request = new HttpGet("http://www.example.com");

    1. make request to the server side

    HttpResponse response;

    try {
    response = client.execute(request);
    
        Log.d("Response of GET request", response.toString());
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (IOException e) {
          // TODO Auto-generated catch block
         e.printStackTrace();
    }
    

    Then depending on the format of your response, you want to parse it. Also don't forget to include necessary permission on your manifest.xml file such as permission to use internet.