Wednesday 21 August 2013

Get json respone using POST method

If you have an web api with json response and with "method = post" and with parameters then do like below

in your oncreate method

GetAllCities getallcities = new GetAllCities();            getallcities.execute(this); 

outside of your oncreate method

@SuppressWarnings("rawtypes")
    public class GetAllCities extends AsyncTask {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            prodialog = new ProgressDialog(mContext);
            prodialog.setMessage("Loading");
            prodialog.show();
            prodialog.setCancelable(false);
        }

        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            String result;
            try {
                JsonHttpPost httpPost = new JsonHttpPost();
           
                result = httpPost.post1();
                Log.i(tag, "result" + result);
               
                cityparser = new CityParser();
                lstcitysetget = cityparser.citysetget(result);
               
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return lstcitysetget;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            prodialog.dismiss();
           // make your adapter here
                }
    } 



put below function in your JosnHTTP class

public String post1() {
        String result = null;
        try {
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(params, TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(params);
            System.out.println("Getting url: ");
            HttpPost post = new HttpPost(serverurl);

            try {
                JSONObject json = new JSONObject();
                json.put("action", "get_all_cities");
                post.setEntity(new ByteArrayEntity(json.toString().getBytes(
                        "UTF8")));

            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }

            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            } else {
                Log.d("Response Failed", "Response from server is failed");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }



this is my parser class

public class CityParser {
   
    public List<String> citysetget(String result) {

        String city_id = "", state_id = "", city_name = "";

        //List<CityGetterSetter> citysetgetlst = new ArrayList<CityGetterSetter>();
        List<String> citysetgetlst = new ArrayList<String>();

        CityGetterSetter citysetget = null;
        try {
            JSONObject jsonObject = new JSONObject(result);

            if (jsonObject != null) {
                JSONArray objarry = jsonObject.optJSONArray("get_all_cities");

                if (objarry != null) {
                    JSONArray strU_ID = jsonObject
                            .getJSONArray("get_all_cities");

                    for (int i = 0; i < strU_ID.length(); i++) {

                        JSONObject obj = strU_ID.getJSONObject(i);
                        if (obj.has("city_id")) {
                            city_id = obj.getString("city_id");
                        }
                        if (obj.has("state_id")) {
                            state_id = obj.getString("state_id");
                        }
                        if (obj.has("city_name")) {
                            city_name = obj.getString("city_name");
                        }
                      
                        citysetget = new CityGetterSetter(city_name);
                    //    citysetgetlst.add(citysetget);
                        citysetgetlst.add(city_name);
                    }
                }
            }
            Log.i("City Paresr...", "city list.."+ citysetgetlst);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return citysetgetlst;
    }
}




If more then one parameter then


    public String postSearch(String city,String purpose,String people) {
        String result = null;
        try {
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(params, TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(params);
            System.out.println("Getting url: ");
            HttpPost post = new HttpPost(serverurlsearch);

            // String body = "{\"action\":\"get_all_cities\"}";
            try {
                JSONObject json = new JSONObject();
                json.put("action", "search_venues_by_purpose");
                json.put("city",city);
                json.put("purpose",purpose);
                json.put("people", people);
              
                post.setEntity(new ByteArrayEntity(json.toString().getBytes(
                        "UTF8")));

            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }

            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            } else {
                Log.d("Response Failed", "Response from server is failed");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }



Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...