Android Simple Http Get Request without any Parameter
String result="";
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet link = new HttpGet("http://12.133.183.155:8081/MCP/media/getMediaItems?page=1&pagesize=1");
// Execute the request
HttpResponse response;
try
{
response = httpclient.execute(link);
// Examine the response status
Log.d("Response Status", response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if(entity != null){
// A Simple JSON Response Read
InputStream instream = entity.getContent();
result= convertStreamToString(instream);
Log.i("Response result",result);
// now you have the string representation of the HTML request
instream.close();
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet link = new HttpGet("http://12.133.183.155:8081/MCP/media/getMediaItems?page=1&pagesize=1");
// Execute the request
HttpResponse response;
try
{
response = httpclient.execute(link);
// Examine the response status
Log.d("Response Status", response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if(entity != null){
// A Simple JSON Response Read
InputStream instream = entity.getContent();
result= convertStreamToString(instream);
Log.i("Response result",result);
// now you have the string representation of the HTML request
instream.close();
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
usefull links;
http://www.androidhive.info/2011/10/android-making-http-requests/
http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/