You are on page 1of 3

/**

* Handle Get Request


*
* @param serverURL
* @return
* @throws BaseException
*/
public static String handleGETRequest(String serverURL) throws BaseException
{
int responseCode;
InputStream inputStream = null;
String serverResponseMsg = null;
HttpURLConnection httpURLConnection;
try
{
URL url = new URL(serverURL);
Log.e("URL: ", serverURL);
URLConnection conn = url.openConnection();
httpURLConnection = (HttpURLConnection) conn;
httpURLConnection.setReadTimeout(AppConstant.TIME_CONNECTION_TIMEOUT
);
httpURLConnection.setRequestMethod(GET_METHOD);
httpURLConnection.setRequestProperty("Content-type", "application/xwww-form-urlencoded");
httpURLConnection.connect();
responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200)
{
inputStream = httpURLConnection.getInputStream();
responseCode = httpURLConnection.getResponseCode();
serverResponseMsg = readInputStream(inputStream);
}
if (serverResponseMsg != null)
{
Log.e("RESPONSE " + serverURL, serverResponseMsg);
}
else
{
Log.e("RESPONSE: " + serverURL, responseCode + "");
}
}
catch (MalformedURLException exception)
{
ExceptionManager.dispatchAppExceptionDetails(ErrorCodeConstants.ERRO
R_CODE_1002, exception.toString(), exception);
}
catch (ProtocolException exception)
{
ExceptionManager.dispatchAppExceptionDetails(ErrorCodeConstants.ERRO
R_CODE_1003, exception.toString(), exception);
}
catch (IOException exception)
{
ExceptionManager.dispatchAppExceptionDetails(ErrorCodeConstants.ERRO

R_CODE_1004, exception.toString(), exception);


}
catch (Exception exception)
{
ExceptionManager.dispatchAppExceptionDetails(ErrorCodeConstants.ERRO
R_CODE_1005, exception.toString(), exception);
}
return serverResponseMsg;
}
/**
* Read InputStream
*
* @param inputStream
* @return
* @throws BaseException
*/
private static String readInputStream(InputStream inputStream) throws BaseEx
ception
{
StringBuilder stringBuilder = new StringBuilder();
String subMessage = null;
try
{
InputStreamReader inStreamReader = new InputStreamReader(inputStream
);
BufferedReader bufferReader = new BufferedReader(inStreamReader);
while ((subMessage = bufferReader.readLine()) != null)
{
stringBuilder.append(subMessage);
}
}
catch (IOException exception)
{
ExceptionManager.dispatchAppExceptionDetails(ErrorCodeConstants.ERRO
R_CODE_1006, exception.toString(), exception);
}
catch (Exception exception)
{
ExceptionManager.dispatchAppExceptionDetails(ErrorCodeConstants.ERRO
R_CODE_1007, exception.toString(), exception);
}
return stringBuilder.toString();
}

/**
* CopyInput Stream to Output Stream in Blocks(buffer_size = 1024 bytes)
*
* @param is
* @param os
* @throws IOException
*/
public static void copyStream(InputStream is, OutputStream os) throws IOExce
ption
{

final int buffer_size = 1024;


byte[] bytes = new byte[buffer_size];
for (; ; )
{
// Read byte from input stream
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
// Write byte from output stream
os.write(bytes, 0, count);
}
}
private static class FlushedInputStream extends FilterInputStream
{
public FlushedInputStream(InputStream inputStream)
{
super(inputStream);
}
@Override public long skip(long n) throws IOException
{
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n)
{
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L)
{
int byteToRead = read();
if (byteToRead < 0)
{
break; // we reached EOF
}
else
{
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
}

You might also like