简体   繁体   中英

java.lang.IllegalArgumentException: Illegal character in scheme at index 0:

I am getting an error when I try to run the following code.

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;

    }

I think the error comes in the HttpGet get line.

The logs are as follows

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

Please help me to resolve the issue!!!

Thank you in advance

The single-argument constructor of URI (you passed as argument to HttpGet constructor) does NOT escape illegal characters. You should use the other constructors.

I tried with your code.. here is what i did..

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;
    }



}

and it printed the desired result in logcat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM