为什么代码为以下代码抛出IllegalArgumentException?

时间:2011-12-19 05:00:32

标签: android

我正在尝试从url获取csv文件。但是我收到了错误

  

java.lang.IllegalArgumentException:索引43处查询中的非法字符:

相同的代码适用于其他网址。

try
{
    String result = "";
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://in.finance.yahoo.com/d/quotes.csv?s=^BSESN+^NSEI +LML.NS +LT.NS +RELIANCE.NS +HUL.BO+ONGC.NS+GOLDBEES.BO +ITC.NS +Acc.NS+UNITECH.NS +SPANCO.BO +OCL.NS +LAKSHMIO.BO+POWERGRID.BO+PACIF.BO+COALINDIA.NS+SATYAMCOM.NS+Bhel.ns+SBIN.NS+Plethico.ns+COLPAL.NS+USHAMART.NS+MTNL.NS +RUCHINFRA.NS&f=nl1oc1ghpvjk");
    HttpResponse response = httpClient.execute(httpGet, localContext);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    try
    {
        StringBuffer sb = new StringBuffer();
        String line;
        while((line = reader.readLine())!=null)
        {
            String rowData[] = line.split(",");
            sb.append("\n"+rowData[0]);
        }
        t.setText(sb.toString());
    }
    catch (Exception e) {
        // TODO: handle exception
    }
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

提前致谢..

1 个答案:

答案 0 :(得分:0)

我猜你的网址中可能有一些未编码的符号,在传递它作为参数之前尝试编码你的字符串:

HttpGet httpGet = new HttpGet(Uri.encode("http://in.finance.yahoo.com/d/quotes.csv?s=^BSESN+^NSEI +LML.NS +LT.NS +RELIANCE.NS +HUL.BO+ONGC.NS+GOLDBEES.BO +ITC.NS +Acc.NS+UNITECH.NS +SPANCO.BO +OCL.NS +LAKSHMIO.BO+POWERGRID.BO+PACIF.BO+COALINDIA.NS+SATYAMCOM.NS+Bhel.ns+SBIN.NS+Plethico.ns+COLPAL.NS+USHAMART.NS+MTNL.NS +RUCHINFRA.NS&f=nl1oc1ghpvjk"));