vijay
1/15/2015 - 12:45 PM

Simple AsynTask Using Get Request.

Simple AsynTask Using Get Request.

package com.example.userapp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServiceHandler {

	static String response = null;
	public final static int GET = 1;
	public final static int POST = 2;

	public ServiceHandler() {

	}

	/*
	 * Making service call
	 * 
	 * @url - url to make request
	 * 
	 * @method - http request method
	 */
	public String makeServiceCall(String url, int method) {
		return this.makeServiceCall(url, method, null);
	}

	/*
	 * Making service call
	 * 
	 * @url - url to make request
	 * 
	 * @method - http request method
	 * 
	 * @params - http request params
	 */
	public String makeServiceCall(String url, int method,
			List<NameValuePair> params) {
		try {
			// http client
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpEntity httpEntity = null;
			HttpResponse httpResponse = null;

			// Checking http request method type
			if (method == POST) {
				HttpPost httpPost = new HttpPost(url);
				// adding post params
				if (params != null) {
					httpPost.setEntity(new UrlEncodedFormEntity(params));
				}

				httpResponse = httpClient.execute(httpPost);

			} else if (method == GET) {
				// appending params to url

				HttpGet httpGet = new HttpGet(url);

				httpResponse = httpClient.execute(httpGet);

			}
			httpEntity = httpResponse.getEntity();
			response = EntityUtils.toString(httpEntity);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {

		}

		return response;

	}
}
private class WebService4GettingState extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		  prgDialog = new ProgressDialog(getActivity());
			prgDialog.setMessage("Please Wait...");
			prgDialog.setCancelable(false);
			
		}

		@Override
		protected Void doInBackground(Void... params) {

			Log.d("6jan", "in do in back");

			ServiceHandler serviceHandler_ob = new ServiceHandler();
			String state_json = serviceHandler_ob
					.makeServiceCall(
							"http://crazzybazaar.com/web_service_for_category_listing.php?type=state",
							ServiceHandler.GET);

			Log.d("vktest5jan", "response json:" + state_json);

			if (state_json != null) {
				try {
					JSONArray stateArray = new JSONArray(state_json);

					int len = stateArray.length();
					for (int i = 0; i < len; i++) {
						JSONObject catObj = (JSONObject) stateArray.get(i);
						StateModel stateModel = new StateModel();
						stateModel.setId(catObj.getInt("id"));
						stateModel.setName(catObj.getString("name"));
						state_list.add(stateModel);
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
      if (prgDialog.isShowing()) {
				prgDialog.dismiss();

			}

			
		}

	}