Skip to content

Instantly share code, notes, and snippets.

@Shereef
Created January 29, 2014 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shereef/8685666 to your computer and use it in GitHub Desktop.
Save Shereef/8685666 to your computer and use it in GitHub Desktop.
Android JSON connection
/**
* @param hostNameOrIP
* : the host name or IP<br/>
* ex: google.com
* @param webService
* : the web service name<br/>
* ex: testWCF
* @param classOrEndPoint
* : the file or end point<br/>
* ex: testWCF.svc
* @param method
* : the method being called<br/>
* ex: getUserData
* @param httpMethod
* : the connection method (GET, POST, PUT, DELETE ...etc)
* @param postData
* : the message body (parameters or arguments)
* @return
*/
public static String connectJSON(final String hostNameOrIP,
final String webService, final String classOrEndPoint,
final String method, final String httpMethod, final byte[] postData) {
final String url = "http://" + hostNameOrIP + "/" + webService + "/"
+ classOrEndPoint + "/" + method;
final StringBuffer text = new StringBuffer();
HttpURLConnection conn = null;
OutputStream out = null;
InputStreamReader in = null;
BufferedReader buff = null;
try {
final URL page = new URL(url);
conn = (HttpURLConnection) page.openConnection();
conn.addRequestProperty("Accept", "application/json");
conn.addRequestProperty("Content-Type", "application/json");
if ("GET".equals(httpMethod)) {
conn.connect();
} else {
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod(httpMethod);
out = conn.getOutputStream();
out.write(postData);
out.flush();
out.close();
}
final int responseCode = conn.getResponseCode();
if (responseCode == 401 || responseCode == 403) {
// Authorization Error
Log.e(Constants.TAG, "Authorization error in " + method + "("
+ postData.toString() + ")");
// throw new Exception("Authorization Error in " + method + "("
// + postDataBuilder.toString() + ")");
return null;
}
if (responseCode == 404) {
// Authorization Error
Log.e(Constants.TAG, "Not found error in " + method + "("
+ postData.toString() + ")");
// throw new Exception("Authorization Error in " + method + "("
// + postDataBuilder.toString() + ")");
return null;
}
if (responseCode >= 500 && responseCode <= 504) {
// Server Error
Log.e(Constants.TAG, "Internal server error in " + method + "("
+ postData.toString() + ")");
// throw new Exception("Internal Server Error in " + method +
// "("
// + postDataBuilder.toString() + ")");
return null;
}
in = new InputStreamReader((InputStream) conn.getContent());
buff = new BufferedReader(in);
String line;
while (null != (line = buff.readLine()) && !"null".equals(line)) {
text.append(line + "\n");
}
buff.close();
buff = null;
in.close();
in = null;
conn.disconnect();
conn = null;
} catch (final Exception e) {
Log.e(Constants.TAG,
"Exception while getting " + method + " from " + webService
+ "/" + classOrEndPoint + " with parameters: "
+ postData.toString() + ", exception: "
+ e.toString() + ", cause: " + e.getCause()
+ ", message: " + e.getMessage());
MyMethods.stackTracePrint(e.getStackTrace(), method);
return null;
} finally {
if (null != out) {
try {
out.close();
} catch (final IOException e1) {
}
out = null;
}
if (null != buff) {
try {
buff.close();
} catch (final IOException e1) {
}
buff = null;
}
if (null != in) {
try {
in.close();
} catch (final IOException e1) {
}
in = null;
}
if (null != conn) {
conn.disconnect();
conn = null;
}
}
if (text.length() > 0 && Communications.checkText(text.toString())) {
final String temp = Communications.getDataFromXML(text.toString());
Log.i(Constants.TAG,
"Success in " + method + "(" + postData.toString() + ") = "
+ temp);
return temp;
}
Log.w(Constants.TAG, "Warning: " + method + "(" + postData.toString()
+ "), text = " + text.toString());
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment