Skip to content

Instantly share code, notes, and snippets.

@Shereef
Last active December 13, 2015 22:19
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/4984091 to your computer and use it in GitHub Desktop.
Save Shereef/4984091 to your computer and use it in GitHub Desktop.
A simple way to make connection to a service
package net.shereef.connection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author Shereef Marzouk
*
*/
public class Connection {
private static final String UTF_8 = "UTF-8";
private static final String TAG = "Connection";
/**
* @param args
*/
public static void main(String[] args) {
}
/**
* @param url
* : the url to connect to<br/>
* @param method
* : the method (GET, POST...etc)<br/>
* @param parameters
* : the parameters to be sent in the message body
* @return
*/
public static String connect(final String url, final String method,
final String data) {
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.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(method);
if (!"GET".equals(method) && data && data.length() > 0) {
conn.setDoOutput(true);
out = conn.getOutputStream();
final byte[] postData = data.getBytes(
Connection.UTF_8);
out.write(postData);
out.flush();
out.close();
}
final int responseCode = conn.getResponseCode();
if (responseCode == 401 || responseCode == 403) {
// Authorization Error
Log.e(TAG, "Authorization error in " + url + "("
+ data.toString() + ")");
throw new Exception("Authorization Error in " + url + "("
+ data.toString() + ")");
return null;
}
if (responseCode == 404) {
// Authorization Error
Log.e(TAG, "Not found error in " + url + "("
+ data.toString() + ")");
throw new Exception("Authorization Error in " + url + "("
+ data.toString() + ")");
return null;
}
if (responseCode >= 500 && responseCode <= 504) {
// Server Error
Log.e(TAG, "Internal server error in " + url + "("
+ data.toString() + ")");
throw new Exception("Internal Server Error in " + url +
"("
+ data.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(TAG,
"Exception while getting " + method + " from " + url
+ " with parameters: "
+ data + ", exception: " + e.toString()
+ ", cause: " + e.getCause() + ", message: "
+ e.getMessage());
e.printStackTrace();
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) {
return text.toString();
}
Log.w(TAG,
"Warning: " + url + "(" + data.toString()
+ "), text = " + text.toString());
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment