현재 비동기 http 라이브러리를 사용하여 Google 서버에 대해 HTTP 요청을 실행하고 있습니다. 그러나 이것은 화면 회전 중에 http 호출이 진행 중일 때 호출이 완료되면 이전 컨텍스트에 대한 참조를 갖게되는 문제를 낳습니다. 나는 일종의 onCreate에서 캡처 된 최신 인스턴스에 대한 정적 참조를 유지함으로써이 문제를 해결했으며 해당 참조가있는 메소드를 호출합니다 (onDestroy에서는 null). 괜찮 았지만 해킹처럼 보였습니다. 좋은 생각하지만 난 단순히 내 활동을함으로써이 작업을 수행 할 수있는 생각처럼 보일http 요청에 대한 http 요청에 대해 generics, 상속을 사용하여 방향 변경을 처리합니다.
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
연장 FragmentActivity 사용 : 나는 어떤 사람들은이 다루는 여기처럼하는 조각의 사용을 권장 본 적이 내가하고있는 일을 위해 특별히 의도 된 AsyncTaskLoader 서브 클래스.
내 아이디어는 다음과 같습니다. AsyncTaskLoader with AsiRequest를 구현하고 ApiResponse를 반환합니다. 그러나 나는 응답을 구문 분석하고 ApiResponse를 확장하는 다른 종류의 개체로 변환 할 수 있도록 HttpAsyncTask의 하위 클래스를 만들고 응답을 구문 분석하는 메서드를 재정의 할 수 있기를 원합니다. 이 형식 인수를 지정하는 방법을 잘 모르겠습니다.
public class HttpAsyncTaskLoader</*not sure what to put here*/> extends AsyncTaskLoader<? not sure ?> {
private ApiClient mClient ;
private ApiRequest mRequest;
private volatile boolean isExecuting = false;
public HttpAsyncTaskLoader(Context context, ApiClient client, ApiRequest request) {
super(context);
mClient = client;
mRequest = request;
}
/**
* Subclasses should override this method to do additional parsing
* @param response
* @return
*/
protected /*subclass of ApiResponse (or ApiResponse itself)*/ onResponse(ApiResponse response)
{
//base implementation just returns the value, subclasses would
//do additional processing and turn it into some base class of ApiResponse
return response;
}
@Override
public /** not sure ***/ loadInBackground() {
HttpResponse response = null;
ResponseError error = null;
JSONObject responseJson = null;
ApiResponse apiResponse = null;
try {
isExecuting = true;
//synchronous call
response = mClient.execute(mRequest);
isExecuting = false;
responseJson = new JSONObject(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
error = new ResponseError(e);
} catch (URISyntaxException e) {
error = new ResponseError(e);
} catch (JSONException e) {
error = new ResponseError(e);
} finally {
mClient.getConnectionManager().closeExpiredConnections();
isExecuting = false;
apiResponse = new ApiResponse(getContext().getResources(), response, responseJson, error);
}
return onResponse(apiResponse);
}
@Override
public void onCanceled(ApiResponse response) {
if (isExecuting) {
mClient.getConnectionManager().shutdown();
}
}
}
누구든지 내가이 작업을 수행 할 수있는 방법 아이디어가 : 여기
내 코드? 형식 매개 변수를 지정하는 방법을 잘 모르겠습니다. 나는이 클래스를 유용하게 사용할 수 있기를 바란다. 또한 클래스를 서브 클래스화할 수 있기를 바란다. 요점은 위의 loadInBackground 메서드에서 기능을 다시 구현하고 싶지 않다는 것입니다. 나는 ApiResponse를 제네릭 매개 변수로 사용하고, 기대했던 특정 기본 클래스로 onLoadFinished에 반환 된 ApiResponse 개체를 캐스팅 할 수 있지만,보다 안전한 형식으로이 작업을 수행하고 싶습니다. 또한 나는 본질적으로 동일한 것을 성취하지만 다른 방식으로 성취하는 아이디어에 열려 있습니다.
? 그것은 기본적으로 당신이 원하는 것과 비슷한 방식으로합니다. –
아니요 화면 방향 변경을 처리하지 않습니다. 실제로 위의 작업을 수행 할 방법을 찾았지만 여전히 화면 방향에서 작동하지 않습니다. 토론 할 시간이 조금 있으면 업데이트 할 것입니다. –
실례로 저는 발리가 문제를 해결하는 방법을 모르겠다 고 말하면됩니다. 공유하시기 바랍니다 그리고 나는 당신에게 upvote 줄거야 또는 좋은 해결책이 있다면 받아들이십시오! –