2016-02-29 17 views
2

我已經接管了一個android應用程序,該應用程序可以拍照並將它們附加到公司主基地的較大軟件系統的作業 - 直到最近它一直運行良好。Apache HTTP客戶端僅針對LG G3執行的Android例外6.0

看來,只有對已升級到Android 6.0 LG G3手機有這prodecure異常:

public static String frapiGetRequest(String transaction, ArrayList<Content> parameters) { 
    StringBuilder builder = new StringBuilder(); 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpHost targetHost = new HttpHost(HOST,PORT,SCHEME); 
    String url = SCHEME + "://" + HOST + "/" + transaction; 
    if (parameters != null && parameters.size() > 0) { 
     url += "?" + buildParameterString(parameters); 
    } 

    Utilities.bLog(TAG, "Making FrapiRequest -- " + url); 

    try { 
     HttpGet getRequest = new HttpGet(url); 
     client.getCredentialsProvider().setCredentials(
       new AuthScope(targetHost.getHostName(), targetHost.getPort()), 
       new UsernamePasswordCredentials(USERNAME, PASSWORD)); 

     /**Exception Occurs Here**/ 
     HttpResponse response = client.execute(getRequest); 


     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = -1; 

     statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 
      Utilities.bLog(TAG,"Frapi Request Succeeded"); 
     } 
     else { 
      Utilities.bLog(TAG, "Frapi Request Failed: " + url); 
     } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     Utilities.eLog(e); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Utilities.eLog(e); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Utilities.eLog(e); 
    } 
    return builder.toString(); 
} 

堆棧跟蹤

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 
at org.apache.http.impl.auth.DigestScheme.isGbaScheme(DigestScheme.java:210) 
at org.apache.http.impl.auth.DigestScheme.processChallenge(DigestScheme.java:176) 
at org.apache.http.impl.client.DefaultRequestDirector.processChallenges(DefaultRequestDirector.java:1097) 
at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:980) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:490) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470) 
at com.rossware.sd_quickpics.Utilities.frapiGetRequest(Utilities.java:111) 
at com.rossware.sd_quickpics.Business.authenticate(Business.java:83) 
at com.rossware.sd_quickpics.MainActivity$AuthenticateAsyncTask.doInBackground(MainActivity.java:320) 
at com.rossware.sd_quickpics.MainActivity$AuthenticateAsyncTask.doInBackground(MainActivity.java:307) 
at android.os.AsyncTask$2.call(AsyncTask.java:295) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

這一直沒有報告任何其他電話..我會用HttpURLConnection但它不支持摘要式身份驗證(這是目前我們的frapi服務器正在使用)

我只是不確定是否有任何方法可以繼續使用我們擁有的身份驗證機制,或者如果我必須在frapi中實現不同的協議(希望不會破壞我們現有的所有應用程序..),或者如果有另一種方法用這些手機繞過這個問題的人?這個問題是相當有限的(一個客戶誰擁有大約10個手機,而不是世界末日,但絕對是他們的主要問題)

有什麼在android中,我可以做解決這種問題的受影響用戶?看起來代碼不正確?

+0

有關更多信息,我們將這些交易的frapi身份驗證更改爲public,並且(除了隨機dns問題),在沒有摘要身份驗證的電話上,它似乎正常運行。當然,這意味着交易......並不像我想的那麼安全,但至少是應用程序的功能。 – wizebin

+0

LG G4 Stylus,LG G4和Oneplus One在使用Android 6連接AVM FRITZ!Box路由器時出現同樣的錯誤。我還沒有解決。 – almisoft

+0

@wizebin你能解決這個問題。我也在LG手機上面臨同樣的問題。恰好在相同的地方和相同的錯誤日誌! KIndly讓我知道如果你找到任何解決方案。我也在使用摘要式身份驗證 – Sunil

回答

0

它可以使用DigestAuth與HttpURLConnection類:

private InputStream connect(String urlStr, String username, String password) throws Exception { 

    URL url = new URL(urlStr); 
    HttpURLConnection connection = (HttpURLConnection) new URL(urlStr).openConnection(); 
    connection.setDoInput(true); 
    connection.setRequestMethod("GET"); 

    try { 

     return connection.getInputStream(); 

    } catch(Exception e) { 

     if (connection.getResponseCode() == 401) { 

      String header = connection.getHeaderField("WWW-Authenticate"); 

      String uri = new URL(urlStr).getFile(); 

      String nonce = Tools.match(header, "nonce=\"([A-F0-9]+)\""); 
      String realm = match(header, "realm=\"(.*?)\""); 
      String qop = match(header, "qop=\"(.*?)\""); 
      String algorithm = match(header, "algorithm=(.*?),"); 
      String cnonce = generateCNonce(); 
      String ha1 = username + ":" + realm + ":" + password; 
      String ha1String = md5digestHex(ha1); 
      String ha2 = "GET" + ":" + uri; 
      String ha2String = md5digestHex(ha2); 
      int nc = 1; 
      String response = ha1String + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2String; 
      String responseString = md5digestHex(response); 


      String authorization = 
       "Digest username=\"" + username + "\"" + 
       ", realm=\"" + realm + "\"" + 
       ", nonce=\"" + nonce + "\"" + 
       ", uri=\"" + uri + "\"" + 
       ", qop=\"" + qop + "\"" + 
       ", nc=\"" + nc + "\"" + 
       ", cnonce=\"" + cnonce + "\"" + 
       ", response=\"" + responseString + "\"" + 
       ", algorithm=\"" + algorithm + "\""; 


      HttpURLConnection digestAuthConnection = prepareConnection(urlStr); 
      digestAuthConnection.setRequestMethod("GET"); 
      digestAuthConnection.setRequestProperty("Authorization", authorization); 

      return processResponse(digestAuthConnection); 

     } else throw e; 
    } 
} 

public static String match(String s, String patternString, boolean strict) { 

    if (!isEmpty(s) && !isEmpty(patternString)) { 
     Pattern pattern = Pattern.compile(patternString); 
     if (pattern != null) { 
      Matcher matcher = pattern.matcher(s); 
      if (matcher != null && matcher.find() && (matcher.groupCount() == 1 || !strict)) { 
       return matcher.group(1); 
      } 
     } 
    } 
    return null; 
} 

public static String match(String s, String patternString) { 
    return match(s, patternString, true); 
} 

public static byte[] md5Digist(String s) { 
    try { 
     MessageDigest md5 = MessageDigest.getInstance("md5"); 
     md5.update(s.getBytes()); 
     return md5.digest(); 
    } catch (NoSuchAlgorithmException e) { 
     return null; 
    } 
} 

public static String digest2HexString(byte[] digest) { 

    String digestString=""; 
    int low, hi; 

    for (int i = 0; i < digest.length; i++) { 
     low = (digest[i] & 0x0f) ; 
     hi = ((digest[i] & 0xf0) >> 4); 
     digestString += Integer.toHexString(hi); 
     digestString += Integer.toHexString(low); 
    } 
    return digestString; 
} 

public static String md5digestHex(String s) { 
    return digest2HexString(md5Digist(s)); 
} 

public static String generateCNonce() { 
    String s = ""; 
    for (int i = 0; i < 8; i++) { 
     s += Integer.toHexString(new Random().nextInt(16)); 
    } 

    return s; 

} 
+0

- 無法解析符號'工具' - 無法解析方法'prepareConnection' - 無法解析方法'processResponse' – mattyU

0

我今天遇到了類似的問題,只是使用HttpClient for Android

  1. 添加依賴compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'到的build.gradle開始。
  2. 更換new DefaultHttpClient()HttpClientBuilder.create().build()

有可能是你可能需要在代碼中的其他部分做出一些小的refactors,但應該是相當直截了當。