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:
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!
Did you add this to your manifest
<uses-permission android:name="android.permission.INTERNET" />
You can try it this way maybe using URLConnection class
String error = ""; // string field
private String getDataFromUrl(String demoIdUrl) {
String result = null;
int resCode;
InputStream in;
try {
URL url = new URL(demoIdUrl);
URLConnection urlConn = url.openConnection();
HttpsURLConnection httpsConn = (HttpsURLConnection) urlConn;
httpsConn.setAllowUserInteraction(false);
httpsConn.setInstanceFollowRedirects(true);
httpsConn.setRequestMethod("GET");
httpsConn.connect();
resCode = httpsConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpsConn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
in.close();
result = sb.toString();
} else {
error += resCode;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
I write two method here.Copy both of them.In your first method uri = your url that you want to send request :
First Method:
public static String Getdata (String uri ){
BufferedReader reader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = null;
URL testUrlHttps = new URL(uri);
if (testUrlHttps.getProtocol().toLowerCase().equals("https"))
{
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
con = https;
} else
{
con = (HttpURLConnection) url.openConnection();
}
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
}
and another method that complete trust all certification for first method.
Second method:
private static void trustAllHosts()
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager()
{
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
{
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
{
}
} };
// Install the all-trusting trust manager
try
{
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e)
{
e.printStackTrace();
}
}
Thanks to Make HTTPS / HTTP Request in Android
Add a Java class CustomSSLSocketFactory.java
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLSocketFactory;
public class CustomSSLSocketFactory extends SSLSocketFactory{
SSLContext sslContext = SSLContext.getInstance("TLS");
/**
* Generate Certificate for ssl connection
* @param truststore
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws KeyStoreException
* @throws UnrecoverableKeyException
*/
public CustomSSLSocketFactory(KeyStore truststore)
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] {tm}, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port,
autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
in your code
String cloud_url="https://www.google.com";
HttpClient client = new DefaultHttpClient();
if(cloud_url.toLowerCase().contains("https://")){
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
client= new DefaultHttpClient(ccm, params);
}
HttpGet request= new HttpGet( );
request.setURI(new URI( cloud_url));
HttpResponse response = client.execute(request);
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.