2014-05-17 67 views
-1

我目前正在使用的應用程序是在它的第一個Beta階段,發現這個類在使用公共WiFi時無法完成它的任務。HttpUrlConnection是否必須設置爲使用公共WiFi連接?

有沒有人有過這個問題?與試圖連接的服務器使用的SSL證書有關嗎?

public class NetGameList implements Callable<String> { 

    private DatabaseManager DBM = DatabaseManager.getInstance(); 

    private JSONArray jsonArray; 

    @Override 
    public String call() throws Exception { 

     HttpURLConnection connection; 
     JSONObject jsonParam = new JSONObject(); 

     try { 

      URL url = new URL(Constants.NETBUILDGAMELISTURL); 
      connection = (HttpURLConnection) url.openConnection(); 

      jsonParam.put("TAG", Constants.HTTPTHREE); 
      jsonParam.put("receiverid", Constants.NETUSERFACEBOOKID); 

      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.setUseCaches(false); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Connection", "Keep-Alive"); 
      connection.setRequestProperty("Accept", "application/json"); 
      connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); 

      //write data to server------------------------------------------------------------------------- 
      DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); 

      outputStream.write(jsonParam.toString().getBytes("utf-8")); 

      //flush&close the stream 
      outputStream.flush(); 
      outputStream.close(); 


      //Get result from server 
      InputStream responseStream = new BufferedInputStream(connection.getInputStream()); 

      BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream)); 
      String line = ""; 

      ArrayList<String> jsonArrays = new ArrayList<String>(); 

      while ((line = responseStreamReader.readLine()) != null) { 

       jsonArrays.add(line); 
      } 
      responseStreamReader.close(); 

      for (String game : jsonArrays) { 

       JSONObject jsonObject = new JSONObject(game); 

       jsonArray = jsonObject.getJSONArray("data"); 

       for (int i = 0; i < jsonObject.length(); i++) { 

        JSONObject jObject = jsonArray.getJSONObject(i); 


        if (!DBM.gameExists(jObject.get("GameId").toString())) { 

         DBM.addGame(jObject.get("GameId").toString(), jObject.get("SenderId").toString(), 
           jObject.get("GameTrack").toString(), jObject.get("FileLocation").toString(), getDateTime()); 
        } else { 

         DBM.updateGames(jObject.get("GameId").toString(), jObject.get("SenderId").toString(), 
           jObject.get("GameTrack").toString(), jObject.get("FileLocation").toString(), getDateTime()); 
        } 
       } 
      } 

      responseStream.close(); 
      connection.disconnect(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return Constants.NETOPCOMPLETE; 
    } 

    private String getDateTime() { 
     SimpleDateFormat dateFormat = new SimpleDateFormat(
       "dd/MM/yyyy", Locale.getDefault()); 
     Date date = new Date(); 
     return dateFormat.format(date); 
    } 
} 
+0

我刪除了您的Java標記。這個問題純粹是關於Android,它與Java無關。如果沒有Android經驗,請不要將Java標籤添加到Java開發人員無法回答的問題中。 – drewmoore

+1

@drewmore http://meta.stackoverflow.com/questions/254642/deleting-java-tag-from-questions-tagged-androidjava – laalto

+0

請刪除代碼示例中的垃圾,並保留允許重現該問題的部分。另外,正如我在我的回答中所評論的那樣,不要吞下異常,什麼是日誌,當jsonArrays沒有完成時,它的價值是什麼? – rds

回答

0

要使用WIFI,你應該添加的權限:

<uses-permission android:name="android.permission.INTERNET" /> 

你的清單文件。

+0

清單中的權限是正確的,這只是公共WiFi的問題。感謝您的建議 –

0

有時,「公共」wifi需要基於Web的登錄

+0

這是真的,該設備已登錄訪問通過瀏覽器的WiFi連接。所以我認爲它會允許設備訪問的IP地址?或者是否需要通過addProperty將登錄憑證添加到UrlConnection? –

+0

嗯,我不知道。只需調試jsonArrays的值。不要吞下例外。 – rds

+0

回答「我不知道」,這並不是真的有用。 –

相關問題