Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Sending HTTPS Get Request

I would like to send a HTTPS Get Request to the google shopping api however nothing is quite working for me, for example here is what I'm trying at the moment:

try {        
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy="));
    HttpResponse response = client.execute(request);
} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   
return response;

If anyone has any suggestions on how to improve this or replace it please let me know, thanks in advance.

like image 842
Sam Jackson Avatar asked Apr 01 '12 20:04

Sam Jackson


People also ask

What is http client Android?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.

What is a network call in Android?

Network requests are used to retrieve or modify API data or media from a server. This is a very common task in Android development especially for dynamic data-driven clients. The underlying Java class used for network connections is DefaultHTTPClient or HttpUrlConnection.

What is API call in Android?

An Application Programming Interface (API) is a particular set of rules ('code') and specifications that programs can follow to communicate with each other.


2 Answers

You should be getting a compile error.

This is the correct version:

HttpResponse response = null;
try {        
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy="));
    response = client.execute(request);
} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   
return response;

Therefore now if you have an error your response will be returned as null.

Once you have the response and checked it for null, you'll want to get the content (i.e. your JSON).

http://developer.android.com/reference/org/apache/http/HttpResponse.html http://developer.android.com/reference/org/apache/http/HttpEntity.html http://developer.android.com/reference/java/io/InputStream.html

response.getEntity().getContent();

This gives you an InputStream to work with. If you want to convert this to a string you'd do the below or equivalent:

http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/

public static String convertStreamToString(InputStream inputStream) throws IOException {
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

When you have this string you need to create a JSONObject from it:

http://developer.android.com/reference/org/json/JSONObject.html

JSONObject json = new JSONObject(inputStreamAsString);

Done!

like image 153
Blundell Avatar answered Sep 22 '22 13:09

Blundell


Did you add this to your manifest

<uses-permission android:name="android.permission.INTERNET" />
like image 38
Helal Ismail Avatar answered Sep 24 '22 13:09

Helal Ismail