2014-12-09 4 views
0

parse.com android 응용 프로그램에서 작업 중입니다. 일반적인 http 요청과 마찬가지로 특정 시간에 서버의 응답을받지 못하면 시간 초과 기능이 표시됩니다. 예를 들어 내 코드를 다음과 같은 것입니다 http 요청 시간 초과에 대한 작품 : 나는 때문에 인터넷 연결과 같은 어떤 이유 구문 분석 서버에서 응답을받을 수없는 경우서버 응답 parse.com의 시간 초과 기능 android

  HttpsURLConnection connection = null; 
      connection = (HttpsURLConnection) serverAddress.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setDoOutput(true); 
      connection.setHostnameVerifier(DO_NOT_VERIFY);   
      connection.setReadTimeout(3*1000); 

지금 내가, parse.com에서 동일한 기능을합니다 문제가 발생하면 시간 초과가 발생합니다.

미리 감사드립니다.

답변

3

Parse.com SDK에는 질문에 설명 된 기능이 없습니다.

그러나 예를 들어 ParseQuery에 존재하는 cancel()의 트릭을 사용할 수 있습니다. 따라서 표준 시간 제한보다 적은 시간 초과를 원한다면 백그라운드에서 쿼리를 실행하고 콜백이 쿼리 결과를 전달할 때까지 기다리거나 시간 제한을 구현하면 쿼리 실행이 취소됩니다.

업데이트 :

public class TimeoutQuery<T extends ParseObject> { 
    private ParseQuery<T> mQuery; 
    private final long mTimeout; 
    private FindCallback<T> mCallback; 
    private final Object mLock = new Object(); 
    private final Thread mThread; 


    public TimeoutQuery(ParseQuery<T> query, long timeout) { 
     mQuery = query; 
     mTimeout = timeout; 
     mThread = new Thread() { 
      @Override 
      public void run() { 
       if (isInterrupted()) return; 
       try { 
        Thread.sleep(mTimeout); 
       } catch (InterruptedException e) { 
        return; 
       } 
       cancelQuery(); 
      } 
     }; 
    } 

    private void cancelQuery() { 
     synchronized (mLock) { 
      if (null == mQuery) return; // it's already canceled 
      mQuery.cancel(); 
      new Handler(Looper.getMainLooper()).post(new Runnable() { 
       @Override 
       public void run() { 
        mCallback.done(Collections.<T>emptyList(), new ParseException(ParseException.TIMEOUT, "")); 
       } 
      }); 
     } 
    } 

    public void findInBackground(final FindCallback<T> callback) { 
     synchronized (mLock) { 
      mCallback = callback; 
      mQuery.findInBackground(new FindCallback<T>() { 
       @Override 
       public void done(List<T> ts, ParseException e) { 
        synchronized (mLock) { 
         mThread.interrupt(); 
         mQuery = null; 
         mCallback.done(ts, e); 
        } 
       } 
      }); 
      mThread.start(); 
     } 
    } 
} 

사용법 :

@
ParseQuery<ParseObject> query = ParseQuery.getQuery("Test"); 
new TimeoutQuery<>(query, 1000).findInBackground(new FindCallback<ParseObject>() { 
    ... 
    }); 
+0

무하마드-우스만 - 칸이 추가 코드 – gio