2013-08-22 48 views
2

在我的代碼中,我從URL獲取數據。當URL包含空格時,它在瀏覽器中正常工作,但是當我通過應用程序訪問時,由於URL中的空格而顯示錯誤。如何解決URL中的空間錯誤

我的logcat如下。

http://XXX.XX.XX.XX/~school/index.php/api/index/getschools?mg=Pre-Ks and CDC Lunch 
menu&sl=Elementary 


       try { 

     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams 
       .setConnectionTimeout(client.getParams(), 15000); 
     HttpConnectionParams.setSoTimeout(client.getParams(), 15000); 
     HttpUriRequest request = new HttpGet(SelectMenuAPI); 
     HttpResponse response = client.execute(request); 
     InputStream atomInputStream = response.getEntity().getContent(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       atomInputStream)); 

     String line; 
     String str = ""; 
     while ((line = in.readLine()) != null) { 
      str += line; 
     } 






     } 



    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     // IOConnect = 1; 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 















       /AndroidRuntime(4885): FATAL EXCEPTION: main 
E/AndroidRuntime(4885): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.schoollunchapp/com.schoollunchapp.SecondStep}:  
java.lang.IllegalArgumentException: Illegal character in query at index 69: 
http://XXX.XX.XX.XX/~school/index.php/api/index/getschools?mg=Pre-Ks and CDC Lunch 
    menu&sl=Elementary 

08-23 00:46:18.174: E/AndroidRuntime(4885):  at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 

08-23 00:46:18.174: E/AndroidRuntime(4885):  at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 

08-23 00:46:18.174: E/AndroidRuntime(4885):  at 
android.app.ActivityThread.access$2300(ActivityThread.java:125) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 

08-23 00:46:18.174: E/AndroidRuntime(4885): at  
android.os.Handler.dispatchMessage(Handler.java:99) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at \  
    android.os.Looper.loop(Looper.java:123) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at 
    android.app.ActivityThread.main(ActivityThread.java:4627) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at 
    java.lang.reflect.Method.invokeNative(Native Method) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at 
    java.lang.reflect.Method.invoke(Method.java:521) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at 
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 

    08-23 00:46:18.174: E/AndroidRuntime(4885): at  
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-23 00:46:18.174: E/AndroidRuntime(4885): at  
dalvik.system.NativeStart.main(Native Method) 

08-23 00:46:18.174: E/AndroidRuntime(4885): Caused by: 
java.lang.IllegalArgumentException: Illegal character in query at index 69: 
http://XXX.XX.XX.XX/~school/index.php/api/index/getschools?mg=Pre-Ks and CDC Lunch 
menu&sl=Elementary 
08-23 00:46:18.174: E/AndroidRuntime(4885): at  
    java.net.URI.create(URI.java:970) 
    08-23 00:46:18.174: E/AndroidRuntime(4885): at 
org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75) 

08-23 00:46:18.174: E/AndroidRuntime(4885): at 
com.schoollunchapp.SecondStep.parseJSONData(SecondStep.java:119) 

08-23 00:46:18.174: E/AndroidRuntime(4885): at 
    com.schoollunchapp.SecondStep.onCreate(SecondStep.java:96) 

    08-23 00:46:18.174: E/AndroidRuntime(4885):  at  
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-23 00:46:18.174: E/AndroidRuntime(4885): at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 

回答

2

試試這個行:

String URL = URLEncoder.encode("URL", "UTF-8"); 

String URL = URL.replace(" ", "%20"); 

希望它幫助!

+0

這不工作URL = URLEncoder.encode(SelectMenuAPI,「UTF-8」); – user2706686

+0

嘗試另一個。 –

+1

Ouch ninja'd我的代表:) – sealz

2

用android.replace調用替換%20的空格。

String stringurl = "http://mysite.com/pictures of stuff"; 
stringurl = stringurl.replace(" ", "%20"); 

網址不喜歡空格和填充將擺脫你的問題。這是一個很好的習慣,以便儘可能少地創建站點目錄。

還有其他字符,URL恨和%#的替換。這是由於編碼%,而爲例,書面記錄can be found on this SO post.

+0

此工作thanx – user2706686

相關問題