2013-04-10 65 views
-2

大家好,我有在此解析JSON文件http://ammantimes.net/android/ammantimes.php?at_mod=news 請幫我解決這個問題一個問題:的Android解析JSON:錯誤的HTTP連接java.lang.IllegalArgumentException異常

這裏是我的代碼,我用它來分析它:

public String getJSONTextFromUrl(String url) { 
     // initialize 
     InputStream is = null; 
     String result = ""; 

     // http post 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     } catch (Exception e) { 
      Log.e("log_tag", "Error in http connection " + e.toString()); 
     } 

     // convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = URLDecoder.decode(sb.toString(), "UTF-8"); 
     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     System.out.println("JSON: = " + result); 
     System.out.println("JSON SIZE: = " + result.length()); 

     // return result.replace(result.charAt(0), ' '); 
     return result; 
    } 

public JSONObject getJSONFromScript(String jsonText) { 

     JSONObject jObject = null; 
     String result = getJSONTextFromUrl(jsonText); 

     // try parse the string to a JSON object 
     try { 
      jObject = new JSONObject(result); 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
      e.printStackTrace(); 
     } 

     return jObject; 

    } 

這裏是logcat的輸出和錯誤,我得到:

04-10 14:48:25.070: I/System.out(28470): JSON: = {"news":[{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"}]} 
04-10 14:48:25.070: I/System.out(28470): JSON SIZE: = 755 


04-10 14:48:25.070: I/APACHE HTTP (thCr=11) - NafHttpAuthStrategyDefault(28470): (thUse=11) NafHttpAuthStrategyDefault() 
04-10 14:48:25.070: E/log_tag(28470): Error in http connection java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {"news":[{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"}]} 
04-10 14:48:25.070: E/log_tag(28470): Error converting result java.lang.NullPointerException 

04-10 14:48:25.070: I/System.out(28470): JSON Text: = 
04-10 14:48:25.070: I/System.out(28470): JSON Text SIZE: = 0 

04-10 14:48:25.070: E/log_tag(28470): Error parsing data org.json.JSONException: End of input at character 0 of 
04-10 14:48:25.070: W/dalvikvm(28470): threadid=9: thread exiting with uncaught exception (group=0x4001e578) 
04-10 14:48:25.070: E/AndroidRuntime(28470): FATAL EXCEPTION: Thread-11 
04-10 14:48:25.070: E/AndroidRuntime(28470): java.lang.NullPointerException 
04-10 14:48:25.070: E/AndroidRuntime(28470): at com.jordaneaucation.json.JSONParser.getNewsMap(JSONParser.java:59) 
04-10 14:48:25.070: E/AndroidRuntime(28470): at com.jordaneaucation.ui.NewsTab$1.run(NewsTab.java:72) 
04-10 14:48:25.070: E/AndroidRuntime(28470): at java.lang.Thread.run(Thread.java:1019) 
04-10 14:48:31.570: E/WindowManager(28470): Activity com.jordaneaucation.ui.NewsTab has leaked window [email protected] that was originally added here 
04-10 14:48:31.570: E/WindowManager(28470): android.view.WindowLeaked: Activity com.jordaneaucation.ui.NewsTab has leaked window [email protected] that was originally added here 
04-10 14:48:31.570: E/WindowManager(28470):  at android.view.ViewRoot.<init>(ViewRoot.java:273) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.view.Window$LocalWindowManager.addView(Window.java:424) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.Dialog.show(Dialog.java:241) 
04-10 14:48:31.570: E/WindowManager(28470):  at com.jordaneaucation.ui.NewsTab.onCreate(NewsTab.java:44) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.os.Looper.loop(Looper.java:130) 
04-10 14:48:31.570: E/WindowManager(28470):  at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-10 14:48:31.570: E/WindowManager(28470):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-10 14:48:31.570: E/WindowManager(28470):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-10 14:48:31.570: E/WindowManager(28470):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
04-10 14:48:31.570: E/WindowManager(28470):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 
04-10 14:48:31.570: E/WindowManager(28470):  at dalvik.system.NativeStart.main(Native Method) 

這裏是的printStackTrace()

04-10 15:09:58.189: W/System.err(28947): org.json.JSONException: End of input at character 0 of 
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONTokener.syntaxError(JSONTokener.java:446) 
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONTokener.nextValue(JSONTokener.java:93) 
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONObject.<init>(JSONObject.java:154) 
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONObject.<init>(JSONObject.java:171) 
04-10 15:09:58.189: W/System.err(28947): at com.jordaneaucation.json.JSONParser.getJSONFromScript(JSONParser.java:131) 
04-10 15:09:58.189: W/System.err(28947): at com.jordaneaucation.json.JSONParser.getNewsMap(JSONParser.java:54) 
04-10 15:09:58.189: W/System.err(28947): at com.jordaneaucation.ui.NewsTab$1.run(NewsTab.java:72) 
04-10 15:09:58.189: W/System.err(28947): at java.lang.Thread.run(Thread.java:1019) 
04-10 15:09:58.189: W/dalvikvm(28947): threadid=9: thread exiting with uncaught exception (group=0x4001e578) 
+1

-1,則不要繼續處理來自Http連接的結果,以捕獲其中沒有printstacktrace的異常。 – njzk2 2013-04-10 12:11:32

+0

另外,錯誤顯然不是與解析JSON,因爲它在達到該部分之前崩潰的方式 – njzk2 2013-04-10 12:13:17

+0

@ njzk2我添加了打印堆棧跟蹤 – 2013-04-10 12:17:17

回答

0

我通常使用GSON解析JSON數據..

你可以參考 parsing array in gson in java

GSON庫,您可以從 https://code.google.com/p/google-gson/

得到您的項目中添加這些谷歌jar文件

要解析JSON,請使用如下代碼:

connection = (HttpURLConnection) url.openConnection(); 
connection.connect(); 
reader = new InputStreamReader(connection.getInputStream(), charset); 

WebsiteResults results = new Gson().fromJson(reader, WebsiteResults.class); 

WebsiteResults是您將創建用於解析JSON的類,如鏈接中所述。

+0

發佈爲不相關。該錯誤在解析開始之前就已經很久了。 – njzk2 2013-04-10 12:51:50

+0

以及我要求他使用不同的方法,主要目的是解析JSON響應,我只是建議備用解決方案,如果這個問題無法解決... – Sunny 2013-04-10 13:26:28

+0

問題出在url中。您提出的修改是在錯誤發生後發生,意味着它仍然不起作用。 – njzk2 2013-04-10 13:56:00

0

閱讀您的日誌。

這是你的錯誤:

04-10 14:48:25.070: E/log_tag(28470): Error in http connection java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {"news":[{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"}]}

你誤以爲URL和POST參數。在新的HttpPost(url)中,你的url實際上就是這個長的json字符串。 (來自調用方法的jsonText)。

其餘的錯誤來自於您不處理第一個錯誤的事實。您的請求未被執行,is保留爲空,因此在此之後NPE,結果保持空白,您的JSONObject不滿意空參數,結果保留爲空,因此最終NPE。

這是一個典型的「我不在乎異常」的例子,導致傳播一個遠離它的起源的錯誤。不要忽視異常。如果連接失敗(!)

+0

謝謝,我通過使用commons-httpclient庫解決了這個問題,,, GetMethod getMethod = new GetMethod(url); \t \t \t getMethod.setDoAuthentication(true); – 2013-04-10 14:49:53