-2

i have this query

http://mtmis.punjab.gov.pk/Tax Collection/searchNameTemp.jsp?names=%s

 class TrackingNumber extends AsyncTask<String, Void, String> 
 {
        @Override
        protected String doInBackground(String... arg0) 
        {
            String urlString = null;
            try {
                urlString = URLEncoder.encode(String.format(url, search), "utf-8");
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httppost = new HttpGet(urlString);

        try {
            httpclient.getParams().setParameter("http.connection-manager.timeout", 15000); 
            HttpResponse response = httpclient.execute(httppost);

            String html = "";
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null)
            {
                str.append(line);
            }
            in.close();
            html = str.toString();

            return html;
            } catch (ClientProtocolException es) 
            {   
                Log.e("x" , es.getMessage());
                return "";
            } catch (IOException e) 
            {
                Log.e("aasx" , e.getMessage());
                return "";
            }

        }
        @Override
        protected void onPostExecute(String response) 
        {
                String responseConverted = html2text(response);
                Log.e("tag", responseConverted);

        }
 }

My error log

02-06 17:26:35.055: E/AndroidRuntime(1625): FATAL EXCEPTION: AsyncTask #2
02-06 17:26:35.055: E/AndroidRuntime(1625): java.lang.RuntimeException: An error occured while executing doInBackground()
02-06 17:26:35.055: E/AndroidRuntime(1625):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.lang.Thread.run(Thread.java:856)
02-06 17:26:35.055: E/AndroidRuntime(1625): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://mtmis.punjab.gov.pk/Tax+Collection/searchNameTemp.jsp?names=j
02-06 17:26:35.055: E/AndroidRuntime(1625):     at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at com.trib.app.mtis.WebClass$TrackingNumber.doInBackground(WebClass.java:72)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at com.trib.app.mtis.WebClass$TrackingNumber.doInBackground(WebClass.java:1)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-06 17:26:35.055: E/AndroidRuntime(1625):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • where do you reference `arg0[0]` in `doInBackground()`? What is `url` and `search` and where are they defined? – David M Feb 06 '13 at 17:39
  • shouldn't you be passing these strings in to `AsyncTask` in the the `arg0` array?? – David M Feb 06 '13 at 17:41
  • well. your code doesn't work...so i think it does matter. where are `url` and `search` defined? they're not in `TrackingNumber` (that i see) – David M Feb 06 '13 at 17:44
  • Whatever the other issues may be, it seems like the error log is hinting at a problem with your 'httppost' object. It isn't parsing correctly. I found this which might be relevant: http://stackoverflow.com/questions/11272027/getting-target-host-must-not-be-null-or-set-in-parameters-in-http-get-method-for – Todd Sjolander Feb 06 '13 at 17:46

1 Answers1

0

currently you are encoding whole url including host . just encode parameters instead of url as :

String str_url="http://mtmis.punjab.gov.pk/Tax%20Collection/searchNameTemp.jsp?";
String str_params="names=%s";  //<<< queryString which you want to encode
urlString =str_url+URLEncoder.encode(String.format(str_params, search), "utf-8");
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • parameters are simple currently passing abc. If i don't encode the url it gives error illegal format. check the space between Tax and Collection. – Muhammad Umar Feb 06 '13 at 17:50
  • @MuhammadUmar: then use `%20` instead of space. see my edit answer – ρяσѕρєя K Feb 06 '13 at 17:52
  • perfect i changed the %20 but still giving error forgot to change String.format. Nevermind its fixed now :) ty – Muhammad Umar Feb 06 '13 at 17:59
  • @MuhammadUmar : means im sure this will work because currently in your code `URLEncoder.encode` replacing space with `+ sign` as u can also see in log which is not valid url because spaces always in url replaced by `%20` instead of `+` – ρяσѕρєя K Feb 06 '13 at 18:03