title: Json parsing:Service handling Exception in Android
body: <p>I have been feching datas from url.Depending on first url,second will works and depending on the second third will work.I have been set all datas into spinner when i  modify the first spinner the other two spinner modifies and shows an error.</p>
  
  <pre><code>03-28 11:17:38.958: D/dalvikvm(659): GC_FOR_MALLOC freed 8320 objects / 625048 bytes in 63ms
  03-28 11:17:38.958: E/AndroidRuntime(659): FATAL EXCEPTION: AsyncTask #2
  03-28 11:17:38.958: E/AndroidRuntime(659): java.lang.RuntimeException: An error occured while executing doInBackground()
  03-28 11:17:38.958: E/AndroidRuntime(659):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.lang.Thread.run(Thread.java:1096)
  03-28 11:17:38.958: E/AndroidRuntime(659): Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 59: http://192.169.11.175/AndroidService/Restful.svc/getarea/80 Feet Road
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.net.URI.create(URI.java:970)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at org.apache.http.client.methods.HttpGet.&lt;init&gt;(HttpGet.java:75)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at com.example.testspinner.ServiceHandler.makeServiceCall(ServiceHandler.java:67)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at com.example.testspinner.ServiceHandler.makeServiceCall(ServiceHandler.java:33)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at com.example.testspinner.HomeActivity$GetArea.doInBackground(HomeActivity.java:192)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at com.example.testspinner.HomeActivity$GetArea.doInBackground(HomeActivity.java:1)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
  03-28 11:17:38.958: E/AndroidRuntime(659):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  03-28 11:17:38.958: E/AndroidRuntime(659):  ... 4 more
  </code></pre>
  
  <p><strong>HomeActivity.class</strong></p>
  
  <pre><code>public class HomeActivity extends Activity {
      private static String state_url = "/AndroidService/restful.svc/GetState/";
      private static String city_url = "/AndroidService/restful.svc/Getcity/";
      private static String city_area = "/AndroidService/Restful.svc/getarea/";
      Spinner sp_state,sp_city,sp_area,sp_pin;
      ArrayList&lt;String&gt; state_name,state_id;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home);
          sp_state = (Spinner)findViewById(R.id.spinner_state);
          sp_area = (Spinner)findViewById(R.id.spinner_area);
          sp_city = (Spinner)findViewById(R.id.spinner_city);
          sp_pin = (Spinner)findViewById(R.id.spinner_pin);
  
          new GetState(HomeActivity.this).execute();
          sp_state.setOnItemSelectedListener(new OnItemSelectedListener() 
          {
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1,int pos, long arg3) 
              {
                  String item = sp_state.getSelectedItem().toString();
                  if(!item.equals("select"))
                  {
                      new GetCity(HomeActivity.this, state_id.get(pos)).execute();
                  }
  
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                  // TODO Auto-generated method stub
  
              }
          });
  
          sp_city.setOnItemSelectedListener(new OnItemSelectedListener() {
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1,int pos, long arg3) {
                  // TODO Auto-generated method stub
                  String name = sp_city.getItemAtPosition(pos).toString();
                  if(!name.equals("select"))
                  {
                  new GetArea(HomeActivity.this, name).execute();
                  }
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                  // TODO Auto-generated method stub
  
              }
          });
  
      }
  
  
  
      class GetState extends AsyncTask&lt;Void, Void, Void&gt;
      {
          Context con;    
          public GetState(Context c) 
          {
              this.con = c;
          }
          @Override
          protected Void doInBackground(Void... params) 
          {
              ServiceHandler sh = new ServiceHandler();
              String jsonStr = sh.makeServiceCall(state_url, ServiceHandler.GET);
              if(jsonStr!=null)
              {
                  try
                  {
                      state_name = new ArrayList&lt;String&gt;();
                      state_id = new ArrayList&lt;String&gt;();
                      state_name.add("select");
                      state_id.add("select");
                      JSONArray ary = new JSONArray(jsonStr);
                      for(int i=0;i&lt;ary.length();i++)
                      {
                          String name = ary.getJSONObject(i).getString("StateName");
                          int city_id = ary.getJSONObject(i).getInt("PkId");
                          state_name.add(name);
                          state_id.add(""+city_id);
                      }
                  }
                  catch(Exception e)
                  {
                      e.printStackTrace();
                  }
              }
              else
              {
                  Log.e("ServiceHandler", "Couldn't get any data from the url");
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              // TODO Auto-generated method stub
              super.onPostExecute(result);
              ArrayAdapter&lt;String&gt; state_Adapter = new ArrayAdapter&lt;String&gt;(con, R.layout.custom_layout, R.id.textView_custom, state_name);
              sp_state.setAdapter(state_Adapter);
          }
  
      }
  
  
      class GetCity extends AsyncTask&lt;Void, Void, Void&gt;
      {
          Context con;
          String state_id;
          ArrayList&lt;String&gt; mycity;
          public GetCity(Context c,String id) {
              this.con = c;
              this.state_id = id;
          }
          @Override
          protected Void doInBackground(Void... params) {
              ServiceHandler sh = new ServiceHandler();
              String jsonStr = sh.makeServiceCall(city_url+""+state_id, ServiceHandler.GET);
  
              if(jsonStr!=null)
              {
                  try
                  {
                      JSONArray myary = new JSONArray(jsonStr);
                      mycity = new ArrayList&lt;String&gt;();
                      mycity.add("select");
                      for(int i=0;i&lt;myary.length();i++)
                      {
  
                          String city = myary.getJSONObject(i).getString("City");
                          mycity.add(city);
                      }
                  }
                  catch(Exception e)
                  {
                      e.printStackTrace();
                  }
              }
              else 
              {
                  Log.e("ServiceHandler", "Couldn't get any data from the url");
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              // TODO Auto-generated method stub
              super.onPostExecute(result);
              ArrayAdapter&lt;String&gt; state_Adapter = new ArrayAdapter&lt;String&gt;(con, R.layout.custom_layout, R.id.textView_custom, mycity);
              sp_city.setAdapter(state_Adapter);
          }
  
      }
  
      class GetArea extends AsyncTask&lt;Void, Void, Void&gt;
      {
          Context con;
          String name;
          ArrayList&lt;String&gt; myarea;
          public GetArea(Context c,String name) {
              this.con = c;
              this.name = name;
          }
          @Override
          protected Void doInBackground(Void... params) {
  
              ServiceHandler sh = new ServiceHandler();
              String jsonStr = sh.makeServiceCall(city_area+""+name, ServiceHandler.GET);
              Log.d("CityResponse: ", "&gt; " + jsonStr);
              if(jsonStr!=null)
              {
                  try
                  {
                      JSONArray myary = new JSONArray(jsonStr);
                      myarea = new ArrayList&lt;String&gt;();
                      myarea.add("select");
                      for(int i=0;i&lt;myary.length();i++)
                      {
  
                          String city = myary.getJSONObject(i).getString("Area");
                          Log.d("city", city);
  
                          myarea.add(city);
                      }
                  }
                  catch(Exception e)
                  {
                      e.printStackTrace();
                  }
              }
              else 
              {
                  Log.e("ServiceHandler", "Couldn't get any data from the url");
              }
  
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              // TODO Auto-generated method stub
              super.onPostExecute(result);
              ArrayAdapter&lt;String&gt; state_Adapter = new ArrayAdapter&lt;String&gt;(con, R.layout.custom_layout, R.id.textView_custom, myarea);
              sp_area.setAdapter(state_Adapter);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>ServiceHandler.class</strong></p>
  
  <pre><code>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&lt;NameValuePair&gt; 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
                  if (params != null) {
                      String paramString = URLEncodedUtils
                              .format(params, "utf-8");
                      url += "?" + paramString;
                  }
                  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) {
              e.printStackTrace();
          }
  
          return response;
  
      }
  }
  </code></pre>