Pages

Thursday, February 25, 2016

json

 private static String convertInputStreamToString(InputStream inputStream) throws IOException{
       BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
       String line = "";
       String result = "";
       while((line = bufferedReader.readLine()) != null)
           result += line;

       inputStream.close();
   
     
       return result;

   }


public static String fetchGetResponseFromServer(String url){
       InputStream inputStream = null;
       String result = "";
       try {

           // create HttpClient
           HttpClient httpclient = new DefaultHttpClient();

           // make GET request to the given URL
           HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

           // receive response as inputStream
           inputStream = httpResponse.getEntity().getContent();

           // convert inputstream to string
           if(inputStream != null)
               result = convertInputStreamToString(inputStream);
           else
               result = "Did not work!";

       } catch (Exception e) {
           Log.d("InputStream", e.getLocalizedMessage());
       }

       return result;
   }





private class HttpAsyncTask extends AsyncTask {




@Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub

String ret =  fetchGetResponseFromServer(urls[0]);

ret= ret.substring(15,ret.length()-1);
httpResult = ret;
return ret;

}

}


to call
 new HttpAsyncTask().execute("url");

tools:listitem="@layout/list_item"