2015-11-29 117 views
1

我有簡單的HttpUrlConnection發佈請求。它去一個PHP文件,然後查找數據,如果數據不存在,PHP文件回聲「N」,如果它在那裏的PHP文件回聲數據..而在我的Java代碼中,我使用檢查,看看是否在處理數據之前響應爲「N」。但是,即使N ..中的響應導致應用程序崩潰,數據也始終得到處理。繼承人我的代碼和錯誤的logcat:請幫助:)HttpUrlConnection響應比較失敗

Java代碼

new Thread(new Runnable() { 
     @Override 
     public void run() { 

      OutputStream os = null; 
      HttpURLConnection con = null; 
      String cRE; 
      try { 

       Log.e("DATA", getArti); 

       //constants 
       URL url = new URL("http:www.url.com/php/getarticles.php"); 
       JSONObject jsonObject = new JSONObject(); 
       jsonObject.put("req", type + ";" + getArti + extraNetworkData); 
       String message = jsonObject.toString(); 

       con = (HttpURLConnection) url.openConnection(); 
       con.setReadTimeout(10000); 
       con.setConnectTimeout(15000); 
       con.setRequestMethod("POST"); 
       con.setDoInput(true); 
       con.setDoOutput(true); 
       con.setFixedLengthStreamingMode(message.getBytes().length); 

       //make some HTTP header nicety 
       con.setRequestProperty("Content-Type", "application/json;charset=utf-8"); 
       con.setRequestProperty("X-Requested-With", "XMLHttpRequest"); 

       //open 
       con.connect(); 

       //setup send 
       os = new BufferedOutputStream(con.getOutputStream()); 
       os.write(message.getBytes()); 
       //clean up 
       os.flush(); 

       int responseCode = con.getResponseCode(); 

       if (responseCode == HttpsURLConnection.HTTP_OK) { 

        Log.e("RESPONSE STATUS", "ITS HTTP_OK"); 
        String line; 
        InputStream iStream = con.getInputStream(); 
        BufferedReader brr = new BufferedReader(new InputStreamReader(iStream)); 
        brr.mark(0); 
        while ((line = brr.readLine()) != null) { 
         cRE = line; 

         Log.e("Res", cRE); // RESPONSE 

         if (type.equals("FT")) { 

          String data[] = cRE.split("-"); 
          title = data[0]; 
          image = data[1]; 
          id = data[2]; 
          dateNumber = data[3]; 
         } else if (type.equals("R")) { // R = REFRESH DATA 

          if(cRE.equals("N")) { 

           Log.e("REQUEST DATA RESPONSE", "R Data = NULL"); 
          } else { 

           String data[] = cRE.split("-"); 
           title = data[0]; 
           image = data[1]; 
           id = data[2]; 
           dateNumber = data[3]; 
          } 
         } else if(cRE.equals("SC")) { // SC = SCROLL DOWN DATA 

          if(cRE.equals("N")) { 

          } else { 

           String data[] = cRE.split("-"); 
           title = data[0]; 
           image = data[1]; 
           id = data[2]; 
           dateNumber = data[3]; 
          } 
         } 

        } 

        processNetworkData(); 

       } else { 
        Log.e("RESPONSE ERR", con.getResponseMessage()); 

       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } finally { 

       con.disconnect(); 
      } 
     } 

    }).start(); 

PHP代碼:

mysqli_stmt_bind_result($stmt, $aDate, $id, $title, $image); 

    $ad = ""; 
    $d = ""; 
    $tit = ""; 
    $ima = ""; 

    if(is_null(mysqli_stmt_fetch($stmt))) { 

     echo "N"; 

    } else { 

     /* Fetch value */ 
     while(mysqli_stmt_fetch($stmt)) { 

     $ad = $ad ."". "$aDate;"; 
     $d = $d ."". "$id;"; 
     $tit = $tit ."". "$title;"; 
     $ima = $ima ."". "$image;"; 

     } 

     $aDa = substr($ad, 0, -1); 
     $aId = substr($d, 0, -1); 
     $aTitle = substr($tit, 0, -1); 
     $aImage = substr($ima, 0, -1); 

     echo "$aTitle-$aImage-$aId-$aDa"; 

     /* close statement */ 
     mysqli_stmt_close($stmt); 
    } 

logcat的錯誤:

11-29 16:17:30.397 17160-31330/com.android.appname E/RESPONSE STATUS: ITS HTTP_OK 
11-29 16:17:30.398 17160-31330/com.android.appname E/Res: N  
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: FATAL EXCEPTION: Thread-7023 
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: Process: com.android.appname, PID: 17160 
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime:  at com.android.appname.Pages.Articles.AllArticlesFragment$3.run(AllArticlesFragment.java:302) 
11-29 16:17:30.399 17160-31330/com.android.appname E/AndroidRuntime:  at java.lang.Thread.run(Thread.java:818) 

回答

2

框架可以處理"emptyNess"爲你..

當你可以配置View層爲你處理'空'時,爲什麼要涉及邊緣情況和所有這些應用程序級邏輯?

你可能會覺得這個作爲一個一般模式...

Server/PHP Input POST Url/parms

Server Output : JSON

Network :: Http

client Async Req/Response Handler

JsonObject --> ViewHolder

ViewHolder --> ListView 

    ListView configured for "empty" case 

example of above

想想PHP重構。

國際海事組織,如果服務器返回JSON對象的邊緣情況下(空)只是一個空的根'結果'裏面的json.Array?與在客戶端呈現JSON作爲處理http響應的一部分相比,這似乎更容易和更好地區分問題。

如果您刪除客戶負責採取低級別http.response並從中創建JSON,測試更容易和更清潔。

如果你這樣做,那麼你的客戶端代碼可以更短/更簡單。即在Android中,您可以從PHP返回的Json構建視圖。