我有这个查询
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);
}
}
我的错误日志
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)
答案 0 :(得分:0)
目前您正在编码整个网址,包括主机。只需编码参数而不是url:
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");