HTTP Request
package com.sollet.buzz.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
public class HttpRequest {
/**
* Request Handler Here the Request is in JSON Format
*
* @param requestURL
* @param xmlRequest
* @return
*/
public Hashtable
UiUtility.printMe("Inside HTTP Request Handler ");
UiUtility.printMe("ReqURL:" + requestURL);
UiUtility.printMe("XMLReq:" + xmlRequest);
Hashtable
URL url = null;
HttpURLConnection urlConnection = null;
InputStream iStrm = null;
int reqResponsecode = 300;
if (requestURL == null || requestURL.equals("")) {
response_Ht.clear();
response_Ht.put("STATUS", "" + reqResponsecode);
response_Ht.put("REASON", "PASSED URL IS NULL");
return response_Ht;
}
try {
ConnectivityManager connec = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED
|| connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) {
UiUtility.printMe("is connected");
// String encodedUrl = URLEncoder.encode(requestURL, "UTF-8");
url = new URL(requestURL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
if (xmlRequest != null) {
OutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.write(xmlRequest.getBytes());
out.flush();
out.close();
}
// get the response if respons code =200 it goes to loop
// otherwise
// returns response code
reqResponsecode = urlConnection.getResponseCode();
if (reqResponsecode == HttpURLConnection.HTTP_OK) {
iStrm = urlConnection.getInputStream();
int rsplength = (int) urlConnection.getContentLength();
if (rsplength == 0) {
response_Ht.clear();
response_Ht.put("STATUS", "300");
response_Ht.put("REASON",
"Server Exception : Response Length Zero");
} else {
try {
response_Ht.clear();
StringBuffer parsingSting = new StringBuffer();
int c = 0;
while (((c = iStrm.read()) != -1)) {
parsingSting.append((char) c);
}
JSONObject mainJson = new JSONObject(
parsingSting.toString());
Iterator keys = mainJson.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
String value = mainJson.getString(key);
if (key != null) {
key = key.toUpperCase();
}
response_Ht.put(key, value);
}
// END of pull Parser
} catch (Exception e) {
response_Ht.clear();
response_Ht.put("STATUS", "300");
response_Ht.put("REASON",
"JSON Parsing Error:" + e.getMessage());
}
}
} else if (reqResponsecode == 500) {
response_Ht.clear();
response_Ht.put("STATUS", "500");
response_Ht
.put("REASON",
"Server Response Message : Service Down \n Please Try Later");
} else if (reqResponsecode == 504) {
response_Ht.clear();
response_Ht.put("STATUS", "504");
response_Ht
.put("REASON",
"Server Response Message : Request Timed Out \n Please Try Later");
} else {
response_Ht.clear();
response_Ht.put("STATUS", "" + reqResponsecode);
response_Ht.put("REASON", "Server Response Message :"
+ urlConnection.getResponseMessage());
}
} else {
response_Ht.clear();
response_Ht.put("STATUS", "0110");
response_Ht.put("REASON",
"Please Switch On The INTERNET CONNECTION");
}
} catch (Exception e) {
response_Ht.clear();
response_Ht.put("STATUS", "" + reqResponsecode);
response_Ht.put("REASON", "Http Error :" + e.getMessage());
} finally {
try {
if (iStrm != null) {
iStrm.close();
}
if (urlConnection != null) {
urlConnection.disconnect();
}
} catch (Exception e) {
}
}
UiUtility.printMe("HTTP HashTable:" + response_Ht);
return response_Ht;
}
/**
* Request Handler :Here the Response is in XML Format
*
* @param requestURL
* @param xmlRequest
* @return
*/
public Hashtable
String requestURL, String xmlRequest) {
UiUtility.printMe("Inside HTTP Request Handler ");
UiUtility.printMe("ReqURL:" + requestURL);
UiUtility.printMe("XMLReq:" + xmlRequest);
Hashtable
URL url = null;
HttpURLConnection urlConnection = null;
InputStream iStrm = null;
int reqResponsecode = 300;
if (requestURL == null || requestURL.equals("")) {
response_Ht.clear();
response_Ht.put("STATUS", "" + reqResponsecode);
response_Ht.put("REASON", "PASSED URL IS NULL");
return response_Ht;
}
try {
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED
|| connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) {
url = new URL(requestURL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
urlConnection.setDoInput(true);
// Allow Outputs
urlConnection.setDoOutput(true);
// Don't use a cached copy.
urlConnection.setUseCaches(false);
if (xmlRequest != null) {
OutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.write(xmlRequest.getBytes());
out.flush();
out.close();
}
// get the response if respons code =200 it goes to loop
// otherwise
// returns response code
reqResponsecode = urlConnection.getResponseCode();
if (reqResponsecode == HttpURLConnection.HTTP_OK) {
iStrm = urlConnection.getInputStream();
int rsplength = (int) urlConnection.getContentLength();
if (rsplength == 0) {
response_Ht.clear();
response_Ht.put("STATUS", "300");
response_Ht.put("REASON",
"Server Exception : Response Length Zero");
} else {
try {
response_Ht.clear();
XmlPullParserFactory factory = XmlPullParserFactory
.newInstance();
XmlPullParser parser = factory.newPullParser();
Reader iStrm_reader = new InputStreamReader(iStrm);
parser.setInput(iStrm_reader);
int eventType = parser.getEventType();
parser.nextTag();
// parser.require(XmlPullParser.START_TAG, null,
// "SUBK_GATEWAY");
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
if (eventType == XmlPullParser.START_TAG
&& name != null) {
String temp = parser.nextText();
if (name != null) {
name = name.toUpperCase();
}
if (temp == null) {
temp = "";
}
response_Ht.put(name.trim(), temp.trim());
UiUtility.printMe("Name:" + name.trim()
+ "|Value:" + temp.trim());
}
eventType = parser.next();
}
// END of pull Parser
} catch (Exception e) {
response_Ht.clear();
response_Ht.put("STATUS", "300");
response_Ht.put("REASON",
"Parsing Error:" + e.getMessage());
}
}
} else if (reqResponsecode == 500) {
response_Ht.clear();
response_Ht.put("STATUS", "500");
response_Ht
.put("REASON",
"Server Response Message : Service Down \n Please Try Later");
} else if (reqResponsecode == 504) {
response_Ht.clear();
response_Ht.put("STATUS", "504");
response_Ht
.put("REASON",
"Server Response Message : Request Timed Out \n Please Try Later");
} else {
response_Ht.clear();
response_Ht.put("STATUS", "" + reqResponsecode);
response_Ht.put("REASON", "Server Response Message :"
+ urlConnection.getResponseMessage());
}
} else {
response_Ht.clear();
response_Ht.put("STATUS", "0110");
response_Ht.put("REASON",
"Please Switch On The INTERNET CONNECTION");
}
} catch (Exception e) {
response_Ht.clear();
response_Ht.put("STATUS", "" + reqResponsecode);
response_Ht.put("REASON", "Http Error :" + e.getMessage());
} finally {
try {
if (iStrm != null) {
iStrm.close();
}
if (urlConnection != null) {
urlConnection.disconnect();
}
} catch (Exception e) {
}
}
UiUtility.printMe("HTTP HashTable:" + response_Ht);
return response_Ht;
}
}
No comments:
Post a Comment