jeudi 12 mars 2015

Android: Getting HTTP 403 Forbidden upon calling http://ift.tt/1jq34OV



I have been trying to use the Google api, more specifically this URL to get the U.S State and city and I keep on getting a 403 Forbidden HTTP error message, My code goes as follows:



private static boolean getAddressResult(String input, StringBuilder jsonResults) {
HttpURLConnection conn = null;
try {

String mapUrl;
StringBuilder sb = new StringBuilder(http://ift.tt/1jq34OV);
sb.append("?sensor=false&address=" + URLEncoder.encode(input, "utf8"));

mapUrl = sb.toString();

URL url = new URL(mapUrl);

Logger.d(TAG, ""+ url);
Logger.d(TAG, "trying to read");

conn = (HttpURLConnection) url.openConnection();


int status = conn.getResponseCode();

Logger.d(TAG, "status: "+ status);
InputStreamReader in = new InputStreamReader(conn.getInputStream());

// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
long currentTime = System.currentTimeMillis();

while ((read = in.read(buff)) != -1) {
Logger.d("NetworkUtil", "trying to parse");

long elapsedTime = System.currentTimeMillis();
if(elapsedTime-currentTime>=5000)
return false;
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Logger.e(LOG_TAG, "Error processing Places API URL", e);
return true;
} catch (IOException e) {
Logger.e(LOG_TAG, "Error connecting to Places API", e);
return true;
} finally {
if (conn != null) {
conn.disconnect();
}
}
return false;
}


I have tried different alternatives including changing the URL from HTTP to HTTPS, I am not sure of exactly what I am missing at this point, there have been few instances where this web service call came back with a 200 success but most of the time it just fails with a 403 HTTP ERROR code, any advice would be helpful at this point, I am also attaching a Log from Logcat:



Error connecting to Places API
java.io.FileNotFoundException: http://ift.tt/1Cb9Wwy
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
at .getAddressResult(NetworkUtil.java:217)
at .access$000(NetworkUtil.java:30)
at $ResolveCityTask.doInBackground(NetworkUtil.java:81)
at $ResolveCityTask.doInBackground(NetworkUtil.java:42)



Aucun commentaire:

Enregistrer un commentaire