Skip to content

Instantly share code, notes, and snippets.

@Shereef
Last active August 29, 2015 14:01
Show Gist options
  • Save Shereef/dd1df809b39bb114bc8b to your computer and use it in GitHub Desktop.
Save Shereef/dd1df809b39bb114bc8b to your computer and use it in GitHub Desktop.
Communication Utilities By Shereef Marzouk
/**
* @author Shereef Marzouk
*/
public class CommUtils {
/**
* @param hostNameOrIP
* : the host name or IP<br/>
* @param webService
* : the web service name<br/>
* @param classOrEndPoint
* : the file or end point<br/>
* @param method
* : the method being called<br/>
* @param httpMethod
* : the connection method (GET, POST, PUT, DELETE ...etc)
* @param postData
* : the message body (parameters or arguments)
* @param contentType
* @return
*/
public static String connectData(final String url, final String httpMethod, final String contentType, final byte[] postData) {
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();
if (!TextUtils.isEmpty(contentType)) {
conn.addRequestProperty("Content-Type", contentType);
}
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)) {
Crashlytics.setInt("responseCode", responseCode);
Crashlytics.setString("url", url);
Crashlytics.setString("httpMethod", httpMethod);
Crashlytics.setString("postData", Arrays.toString(postData));
Crashlytics.setString("contentType", contentType);
AnalyticUtils.logException("Bad response code in connectData");
return null;
}
if (responseCode == 404) {
Crashlytics.setInt("responseCode", responseCode);
Crashlytics.setString("url", url);
Crashlytics.setString("httpMethod", httpMethod);
Crashlytics.setString("postData", Arrays.toString(postData));
Crashlytics.setString("contentType", contentType);
AnalyticUtils.logException("Bad response code in connectData");
return null;
}
if ((responseCode >= 500) && (responseCode <= 504)) {
Crashlytics.setInt("responseCode", responseCode);
Crashlytics.setString("url", url);
Crashlytics.setString("httpMethod", httpMethod);
Crashlytics.setString("postData", Arrays.toString(postData));
Crashlytics.setString("contentType", contentType);
AnalyticUtils.logException("Bad response code in connectData");
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) {
e.printStackTrace();
Crashlytics.setString("url", url);
Crashlytics.setString("httpMethod", httpMethod);
Crashlytics.setString("postData", Arrays.toString(postData));
Crashlytics.setString("contentType", contentType);
Crashlytics.logException(e);
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;
}
}
final String temp = text.toString();
if (text.length() > 0) {
return temp;
}
return null;
}
/**
* Connects to the web service and returns the pure string returned, <br/>
* <b>NOTE: if the generated URL is more than 1024 it automatically
* delegates to connectPOST</b>
*
* @param hostName
* : the host name or IP<br/>
* @param webService
* : web service name<br/>
* @param classOrEndPoint
* : file or end point<br/>
* @param method
* : method being called<br/>
* @return the trimmed String received from the web service
* @author Shereef Marzouk
*/
public static String connectGET(final String url) {
final StringBuffer text = new StringBuffer();
HttpURLConnection conn = null;
InputStreamReader in = null;
BufferedReader buff = null;
try {
final URL page = new URL(url);
conn = (HttpURLConnection) page.openConnection();
// conn.addRequestProperty("Accept", "*/*");
// conn.addRequestProperty("Accept-Encoding", "gzip, deflate");
// conn.addRequestProperty("User-Agent",
// "JeddahFood/3.0 CFNetwork/672.0.8 Darwin/14.0.0");
// conn.addRequestProperty("Content-Type", "text/xml");
conn.connect();
in = new InputStreamReader((InputStream) conn.getContent());
buff = new BufferedReader(in);
String line;
while ((null != (line = buff.readLine())) && !"null".equals(line)) {
text.append(line + "\n");
}
} catch (final Exception e) {
e.printStackTrace();
return null;
} finally {
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;
}
}
final String temp = text.toString();
if (text.length() > 0) {
return temp;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment