Try to get this code.
public class UHttpClient {
private HttpClient httpClient = null;
public static final int OK = 200;
public static final int NO_CONTENT = 204;
private static final int CONNECTION_TIMEOUT = 5000;
/* timeout for waiting for data, since once connection is established then waiting for response*/
private static final int SOCKET_TIMEOUT = 20000;
public UHttpClient(UAppInstance appInstance, String loginURL, String deviceID)
throws UBaseException {
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);
//httpClient = new DefaultHttpClient(httpParameters);
httpClient = new DefaultHttpClient();
login(loginURL, deviceID);
}
/**
* Makes the GET call
* @param urlStr - Url string
* @param paramNames - parameter names
* @param paramValues - parameter values
* @return - response body string
* @throws UBaseException
*/
public String executeGet(String urlStr, String[] paramNames, String[] paramValues)
throws UBaseException {
HttpGet httpget = null;
try {
int length = paramNames != null ? paramNames.length : 0;
urlStr = length > 0 ? (urlStr + "?") : urlStr;
for (int k = 0; k < length; k++) {
urlStr = k > 0 ? (urlStr + "&") : urlStr;
urlStr = urlStr + paramNames[k] + "=" + paramValues[k];
}
httpget = new HttpGet(urlStr);
// Execute HTTP Get Request
ResponseHandler<String> respHandler = new BasicResponseHandler();
String response = httpClient.execute(httpget, respHandler);
return response;
} catch (SocketTimeoutException e) {
throw new UBaseException("Connection Timeout", e);
} catch (Exception e) {
if(null == e.getMessage() || "".equals(e.getMessage()))
throw new UBaseException("Unable to connect to server", e);
else
throw new UBaseException(e.getMessage(), e);
}
}
/**
* Makes the POST call
* @param urlStr - Url string
* @param paramNames - parameter names
* @param paramValues - parameter values
* @return - response body string
* @throws UBaseException
*/
public String executePost(String urlStr, String[] paramNames, String[] paramValues)
throws UBaseException {
HttpPost httppost = new HttpPost(urlStr);
try {
int length = paramNames != null ? paramNames.length : 0;
ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();
for (int k = 0; k < length; k++) {
paramsList.add(new BasicNameValuePair(paramNames[k], paramValues[k]));
}
List<NameValuePair> nameValuePairs = paramsList;
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> respHandler = new BasicResponseHandler();
String response = httpClient.execute(httppost, respHandler);
return response;
} catch (SocketTimeoutException e) {
throw new UBaseException("Connection Timeout", e);
} catch (Exception e) {
if(null == e.getMessage() || "".equals(e.getMessage()))
throw new UBaseException("Unable to connect to server", e);
else
throw new UBaseException(e.getMessage(), e);
}
}
protected void login(String url, String deviceID)
throws UBaseException {
// modified by Avishek as in desktop dev instead of distrCode it is entCode
String[] argNames = new String[] {"userDeviceID", "loginMode", "command"};
String[] argValues = new String[] {deviceID, "handheldLogin", "login"};
String response = executePost(url, argNames, argValues);
if(null != response){
response = response.trim();
}
// if response message is not success that means it's a failure so raising exception
if("success".equals(response) == false) {
if(!"".equals(response))
throw new UBaseException(response, null);
else
throw new UBaseException("Remote login failed to '" + url + "'", null);
}
}
}