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.
You should be getting a compile error.
This is the correct version:
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
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/
When you have this string you need to create a JSONObject from it:
http://developer.android.com/reference/org/json/JSONObject.html
Done!
You can try it this way maybe using URLConnection class
Thanks to Make HTTPS / HTTP Request in Android
Add a Java class CustomSSLSocketFactory.java
in your code
works in HttpPost too.
It's hard to say for sure if you don't tell us what the error is.
But if you are running this on the UI thread and the web server takes more than a few seconds to respond you will get an Application Not Responding warning from the system. Make sure that you do any network transfers on a separate thread.
Did you add this to your manifest