Powered By Blogger

Wednesday 6 March 2013

HTTP Connection





public String getProfile() {

Log.d(TAG, "getProfile");

String url


String jsonString = "";

try {

JSONObject json = new JSONObject();

json.put("uid", User.getUserid());

jsonString = getHttpConnection(url, json);

} catch (JSONException e) {

e.printStackTrace();
}

return jsonString;
}



private String getHttpConnection(String url, JSONObject json) {

Log.d(TAG, "getHttpConnection");
Log.d(TAG, "url............ " + url);
Log.d(TAG, "Json.toString() :" + json.toString());

StringBuffer sb2 = new StringBuffer("");

try {

BufferedReader bufferreader = null;

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("json", "["+ json.toString() + "]"));

int timeoutConnection = 0;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);

int timeoutSocket = 0;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
httppost.setEntity(p_entity);
Log.d(TAG, "httppost : " + httppost.toString());

HttpResponse response = client.execute(httppost);

Log.d(TAG, "request executed set ");

InputStream inputstream = response.getEntity().getContent();

Log.d(TAG, "inputstreame set ");

bufferreader = new BufferedReader(
new InputStreamReader(inputstream));

if (inputstream != null) {

bufferreader = new BufferedReader(new InputStreamReader(inputstream));

while ((line = bufferreader.readLine()) != null) {
sb2.append(line);

}
Log.d(TAG, "sb2.toString()" + sb2.toString());

} else {
sb2 = null;
}


System.out.println("Closed input stream:::::::::::::::::::::::::::::::::");
} catch (HttpHostConnectException h) {

Log.e(TAG, "HttpHostConnectException ... ");

} catch (ConnectException e) {

Log.e(TAG, "ConnectException ... ");
Intent i = new Intent(context, ConnectivityManager.class);
context.sendBroadcast(i);
} catch (ConnectionClosedException e) {

Log.e(TAG, "ConnectionClosedException ... ");
Intent i = new Intent(context, ConnectivityManager.class);
context.sendBroadcast(i);
} catch (ConnectTimeoutException e) {
Log.e(TAG, "ConnectTimeoutException ... ");
e.printStackTrace();
return null;
} catch (UnknownHostException e) {

Log.e(TAG, "UnknownHostException ... ");
e.printStackTrace();

} catch (Exception e) {
Log.e(TAG, "GeneralException ... " + e);
e.printStackTrace();

}

return sb2.toString();
}

No comments:

Post a Comment