Question

This code is used to find if a book is available or not.

Now IF the bookname for eg is Analog Electronics

and if i Enter input just as Analog or electronics it will display the full bookname Analog electronics which means that the 1st n 2nd try/catch block were executed properly.

But when i Enter input as Analog electronics with a space in between the two words it delivers an error and says "Check your internet connection " even when my internet connection is proper. It catches the error in the 1st try/catch block which should'nt have happened. I am getting an error all because of the "space".

try {
    response = CustomHttpClient.executeHttpPost(
      "http://duh.com/lol.php?name="+input, 
      postParameters);

    String result = response.toString();

     try{
             returnString = "";
       JSONArray jArray = new JSONArray(result);
             for(int i=0;i<jArray.length();i++){
                     JSONObject json_data = jArray.getJSONObject(i);
                     Log.i("log_tag","ID: "+json_data.getInt("ID")+
                             ", Bookname: "+json_data.getString("Bookname")+

                     );



                     returnString += "\n" + "Book+ json_data.getString("Bookname");

             }

     }
     catch(JSONException e){
         Toast toast = Toast.makeText(getBaseContext(), "Book is not availabe" ,Toast.LENGTH_LONG);
        toast.show();
        toast.getView().postDelayed(new Runnable() {

            @Override
            public void run() {
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();
            }
        }, 3500);
     }

     try{
      tv.setText(returnString);
     }
     catch(Exception e){
         Toast.makeText(getBaseContext(), "Error in Display" ,Toast.LENGTH_LONG).show();;          
     }   
}
      catch (Exception e) {
          Toast toast = Toast.makeText(getBaseContext(), "Check your Internet connection" ,Toast.LENGTH_LONG);
            toast.show();
            toast.getView().postDelayed(new Runnable() {

                @Override
                public void run() {
                    startActivity(new Intent(getBaseContext(), MainActivity.class));
                    finish();
                }
            }, 3500);               
    };

    }

LOG cat error :

04-22 20:51:52.589: E/log_tag(1322): Check your internet connection!!java.lang.IllegalArgumentException: Illegal character in query at index 52: http://duh.com/lol.php?name=analog electonics
04-22 20:51:52.759: W/EGL_emulation(1322): eglSurfaceAttrib not implemented
Was it helpful?

Solution

as per your logcat error:

04-22 20:51:52.589: E/log_tag(1322): Check your internet connection!!java.lang.IllegalArgumentException: Illegal character in query at index 52: http://duh.com/lol.php?name=analog electonics

you have an illegal character in your query.

URLs aren't allowed to have a space character in them without it being encoded (see this page for more details), so you will need to encode your query string before requesting the page.

see this SO answer for the correct way to encode a URL on Android

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top