Created
April 21, 2018 11:48
-
-
Save vishalhalani/af83d07965fa99698ed9f5431a55cacc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HTTPConnectionService { | |
public final static int GET = 1; | |
public final static int POST = 2; | |
HttpURLConnection urlConnection = null; | |
InputStream in = null; | |
// private View view; | |
StringBuilder response = null; | |
// private static final String LINE_FEED = "\r\n"; | |
int resCode = -1; | |
URL murl = null; | |
private OutputStream os; | |
public ServiceHandler() { | |
} | |
/** | |
* Making service call | |
* @url - url to make request | |
* @method - http request method | |
* */ | |
public String makeServiceCall(String url,int method,String rtfa,String fedauth) { | |
return this.makeServiceCall(url, method, null, null, rtfa, fedauth,null,null,false,null,false); | |
} | |
public String makeServiceCall(String url,int method,boolean isContentXml) { | |
return this.makeServiceCall(url, method, null, null, null,null,null,null,false,null,isContentXml); | |
} | |
public String makeServiceCall(String url,int method,String json,boolean isContentXml) { | |
return this.makeServiceCall(url, method, json,null, null, null,null,null,false,null,isContentXml); | |
} | |
public String makeServiceCall(String url,int method,String json,String digest,String rtfa,String fedauth) { | |
return this.makeServiceCall(url, method, json,digest, rtfa, fedauth,null,null,false,null,false); | |
} | |
public String makeServiceCall(String url,int method,String json,String digest,String rtfa,String fedauth,boolean isfileAttached,String filepath) { | |
return this.makeServiceCall(url, method, json,digest, rtfa, fedauth,null,null,isfileAttached,filepath,false); | |
} | |
public String makeServiceCall(String url,int method,String json,String digest,String rtfa,String fedauth,String etag,String httpMethod) { | |
return this.makeServiceCall(url, method, json,digest, rtfa, fedauth,etag,httpMethod,false,null,false); | |
} | |
public String makeServiceCall(String url, int method, | |
String jsondata,String digest,String RTFA,String FedAuth,String eTag,String httpMethod,boolean isFileAttached,String filepath,boolean isContentXml) { | |
response=null; | |
String Status=""; | |
int BUFFER_SIZE = 4096; | |
File uploadFile = null; | |
HttpURLConnection urlConnection = null; | |
try { | |
// http client | |
murl=new URL(url); | |
urlConnection = (HttpURLConnection) murl.openConnection(); | |
if(RTFA != null && FedAuth != null) { | |
urlConnection.addRequestProperty("Cookie", "rtFa=" + RTFA + "; FedAuth=" + FedAuth); | |
} | |
// if file attached to upload | |
if(isFileAttached) { | |
uploadFile = new File(filepath); | |
urlConnection.setRequestProperty("Connection","keep-alive"); | |
} | |
// Checking http request method type | |
if (method == POST) { | |
// if Post Body type is Xml | |
if(isContentXml) | |
{ | |
urlConnection.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); | |
urlConnection.setRequestProperty("Accept", "text/xml; charset=utf-8"); | |
} | |
else | |
{ | |
urlConnection.setRequestProperty("Content-Type", "application/json;odata=verbose"); | |
urlConnection.setRequestProperty("Accept", "application/json;odata=verbose"); | |
} | |
urlConnection.setRequestMethod("POST"); | |
urlConnection.setDoInput(true); | |
urlConnection.setDoOutput(true); | |
if(digest != null) { | |
urlConnection.setRequestProperty("X-RequestDigest", digest); | |
} | |
if(httpMethod!=null) { | |
urlConnection.setRequestProperty("X-HTTP-Method", httpMethod); | |
urlConnection.setRequestProperty("IF-MATCH", eTag); | |
// Log.i("TAG","ETAG=>"+eTag); | |
} | |
if(jsondata != null) { | |
os = urlConnection.getOutputStream(); | |
// if file is attached write file using file input stream | |
if(isFileAttached) | |
{ | |
FileInputStream inputStream = new FileInputStream(uploadFile); | |
byte[] buffer = new byte[BUFFER_SIZE]; | |
int bytesRead = -1; | |
while ((bytesRead = inputStream.read(buffer)) != -1) { | |
os.write(buffer, 0, bytesRead); | |
} | |
os.close(); | |
inputStream.close(); | |
}else { | |
// write post data using buffered writer | |
BufferedWriter writer = new BufferedWriter( | |
new OutputStreamWriter(os, "UTF-8")); | |
writer.write(jsondata); | |
writer.flush(); | |
writer.close(); | |
os.close(); | |
} | |
}else | |
{ | |
os = urlConnection.getOutputStream(); | |
if(isFileAttached) | |
{ | |
FileInputStream inputStream = new FileInputStream(uploadFile); | |
byte[] buffer = new byte[BUFFER_SIZE]; | |
int bytesRead = -1; | |
while ((bytesRead = inputStream.read(buffer)) != -1) { | |
os.write(buffer, 0, bytesRead); | |
} | |
os.close(); | |
inputStream.close(); | |
}else { | |
// if body is xml | |
if(isContentXml) | |
{ | |
File objFile = new File(jsondata); | |
int reqLen = (int) objFile.length(); | |
byte[] reqBytes = new byte[reqLen]; | |
FileInputStream inStream = new FileInputStream(objFile); | |
inStream.read(reqBytes); | |
inStream.close(); | |
os.write(reqBytes); | |
os.flush(); | |
}else | |
{ | |
BufferedWriter writer = new BufferedWriter( | |
new OutputStreamWriter(os, "UTF-8")); | |
writer.write(jsondata); | |
writer.flush(); | |
writer.close(); | |
os.close(); | |
} | |
} | |
} | |
} else if (method == GET) { | |
// appending params to url | |
urlConnection.setRequestMethod("GET"); | |
// if body is xml | |
if(isContentXml) | |
{ | |
urlConnection.setRequestProperty("Accept", "text/xml; charset=utf-8"); | |
}else | |
{ | |
urlConnection.setRequestProperty("Accept", "application/json;odata=verbose"); | |
} | |
} | |
resCode = urlConnection.getResponseCode(); | |
Log.e("WEBCALL", "response code=>" + resCode); | |
Log.e("WEBCALL", "response error=>" + urlConnection.getErrorStream()); | |
InputStream errorstream=urlConnection.getErrorStream(); | |
try { | |
if (errorstream != null) { | |
BufferedReader rd = new BufferedReader(new InputStreamReader( | |
errorstream)); | |
StringBuilder data = new StringBuilder(); | |
String line; | |
while ((line = rd.readLine()) != null) { | |
data.append(line); | |
data.append('\n'); | |
} | |
Log.i("WEBCALL", "error description: "+data); | |
rd.close(); | |
} | |
} catch (MalformedURLException e) { | |
System.out.println("Check URL!!!"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
Log.i("TAG", "response message=>" + urlConnection.getResponseMessage()); | |
// if (resCode == HttpURLConnection.HTTP_FORBIDDEN ) { | |
// // handle unauthorized (if service requires user login) | |
// | |
// Status="Loging Error"; | |
// } else | |
// **************** If Response is Failure ********************* | |
if (resCode != HttpURLConnection.HTTP_ACCEPTED && resCode != HttpURLConnection.HTTP_CREATED && resCode != HttpURLConnection.HTTP_NO_CONTENT && resCode != HttpURLConnection.HTTP_NOT_AUTHORITATIVE && resCode != HttpURLConnection.HTTP_OK ) { | |
// handle any other errors, like 404, 500,.. | |
if(resCode == HttpURLConnection.HTTP_FORBIDDEN || resCode == HttpURLConnection.HTTP_UNAUTHORIZED) | |
{ | |
Status=ApplicationUtil.ERROR_FORBIDDEN; | |
}else | |
{ | |
Status=ApplicationUtil.ERROR; | |
} | |
}else{ | |
// **************** If Response is success ********************* | |
// if (resCode == HttpURLConnection.HTTP_OK) { | |
// Status="Success"; | |
response = new StringBuilder(); | |
// if Response code not equal to no content | |
if(resCode != HttpURLConnection.HTTP_NO_CONTENT) { | |
in = urlConnection.getInputStream(); | |
if (in != null) { | |
BufferedReader r = new BufferedReader(new InputStreamReader(in)); | |
String line; | |
while ((line = r.readLine()) != null) { | |
response.append(line); | |
} | |
Log.i("TAG", "response message=>" + urlConnection.getResponseMessage()); | |
in.close(); | |
} | |
}else | |
{ | |
// if Response code equal to no content | |
response.append(UPDATED); | |
} | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
}finally { | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
} | |
try { | |
// if request is GET | |
if(method==GET) { | |
// if response is failure return error status | |
if(resCode!=HttpURLConnection.HTTP_OK) | |
{ | |
return Status; | |
}else { | |
// if response is success return response string | |
return response.toString(); | |
} | |
}else { | |
// if request is POST | |
if (urlConnection != null) { | |
// if response is success return response string | |
if(resCode==HttpURLConnection.HTTP_CREATED ||resCode==HttpURLConnection.HTTP_ACCEPTED || resCode==HttpURLConnection.HTTP_OK ||resCode==HttpURLConnection.HTTP_NO_CONTENT) { | |
// Log.i("TAG", "RESPONSE=>" + response.toString()); | |
return response.toString(); | |
}else { | |
// if response is failure return error status | |
return Status; | |
} | |
} | |
} | |
}catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
return ApplicationUtil.ERROR; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment