-2

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
langki05
  • 13
  • 9
  • 1
    That try/catch block covers everything, so it could be unrelated to your internet connection. Have you looked to see what exception is getting thrown? – Dan Harms Apr 23 '14 at 00:36
  • @dcharms the 1st exception gets thrown even when the app is connected to the internet. – langki05 Apr 23 '14 at 00:39
  • That is my point. It would help if you provided us with the exception being thrown because an uncaught exception anywhere in that block will be caught in that catch. – Dan Harms Apr 23 '14 at 00:42
  • @langki05 in the final catch clause, add a line to log the exception thrown, eg: log.e("", "exception", e); and then post the logcat output for the exception in your OP – panini Apr 23 '14 at 00:43
  • @panini I have posted the logcat error. – langki05 Apr 23 '14 at 01:05

1 Answers1

1

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

Community
  • 1
  • 1
panini
  • 2,026
  • 19
  • 21
  • Hmmm thanks its making sense now. So now i just have to find out how to encode a space sign. – langki05 Apr 23 '14 at 01:16
  • if you look at the StackOverflow answer I linked, Android has a built in function that will do URL encoding for you – panini Apr 23 '14 at 01:17