• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

connectivity through json

public static JSONObject connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
JSONObject json = new JSONObject();

try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);

json=new JSONObject(result);

instream.close();
}


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return json;
}


////////////////////////////////////for getting data in json arry formate


public static JSONArray connectArray(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
// JSONObject json = new JSONObject();
JSONArray jsonArray= new JSONArray();
InputStream instream=null;
try {
response = httpclient.execute(httpget);


HttpEntity entity = response.getEntity();

if (entity != null) {
// A Simple JSON Response Read
instream = entity.getContent();
result= convertStreamToString(instream);


jsonArray= new JSONArray(result);

// json=new JSONObject(result);

instream.close();
}

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();






public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}










































return null;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

return jsonArray;
// return json;
}


public static JSONObject connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
JSONObject json = new JSONObject();

try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);

json=new JSONObject(result);

instream.close();
}


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return json;
}





public static JSONArray connectArray(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
// JSONObject json = new JSONObject();
JSONArray jsonArray= new JSONArray();
InputStream instream=null;
try {
response = httpclient.execute(httpget);


HttpEntity entity = response.getEntity();

if (entity != null) {
// A Simple JSON Response Read
instream = entity.getContent();
result= convertStreamToString(instream);


jsonArray= new JSONArray(result);

// json=new JSONObject(result);

instream.close();
}

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

return jsonArray;
// return json;
}
 
Back
Top Bottom