java.lang.IllegalArgumentException:索引0处的scheme中的非法字符:

时间:2015-09-01 08:44:59

标签: java android

尝试运行以下代码时收到错误。

protected String doInBackground(Void... params) {

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://api.rovicorp.com/TVlistings/v9/listings/linearschedule/360861/info?locale=en-US&duration=30&inprogress=true&apikey=4tquwdg8xfcd3seay9hyfr8b");
        String text = null;

        try {
            HttpResponse response = httpClient.execute(httpGet,localContext);


            HttpEntity entity = response.getEntity();


            text = getASCIIContentFromEntity(entity);

            Log.d(text,"In Doback");


        } catch (Exception e) {
            return e.getLocalizedMessage();
        }

        return text;

    }

我认为错误来自HttpGet get line。

日志如下

09-01 12:22:42.424  10416-10457/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.rest_api, PID: 10416
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:304)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.IllegalArgumentException: Illegal character in scheme at index 0:
    http://api.rovicorp.com/TVlistings/v9/listings/linearschedule/360861/info?locale=en-US&duration=30&inprogress=true&apikey=4tquwdg8xfcd3seay9hyfr8b
            at java.net.URI.create(URI.java:733)
            at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:80)
            at com.example.rest_api.MainActivity$LongRunningGetIO.doInBackground(MainActivity.java:93)
            at com.example.rest_api.MainActivity$LongRunningGetIO.doInBackground(MainActivity.java:66)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            ... 4 more

请帮我解决问题!!!

提前谢谢

2 个答案:

答案 0 :(得分:0)

URI的单参数构造函数(作为参数传递给HttpGet构造函数)不会转义非法字符。你应该使用其他构造函数。

答案 1 :(得分:-1)

我试过你的代码.. 这就是我做的..

class async extends AsyncTask<Void,Void, Void>{

    ProgressDialog pd=null;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd.show();

    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    if(pd!=null)pd.dismiss();
    }


    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet;

            httpGet = new HttpGet("http://api.rovicorp.com/TVlistings/v9/listings/linearschedule/360861/info?locale=en-US&duration=30&inprogress=true&apikey=4tquwdg8xfcd3seay9hyfr8b");


                HttpResponse response = httpClient.execute(httpGet,localContext);

                String responseBody = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

                Log.e("In Doback",responseBody);



        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        return null;
    }



}

并在logcat中打印出所需的结果。