저는 Android 개발에 익숙하지 않고 최근에 MVP 패턴을 올바르게 사용하는 방법을 배우고 있습니다.데이터 다운로드가 완료되면 서버에서 데이터를 전달할 수있는 클래스를 디자인하는 방법
이제 저는 까다로운 문제에 직면 해 있습니다. 희망은 여기에서 도움이되는 제안이나 해결책을 얻을 수 있습니다.
첫째, 여기 내 발표자
public class MVPPresenter {
private MVPView mvpView;
public MVPPresenter(MVPView mvpView) {
this.mvpView = mvpView;
}
public void loadData() {
mvpView.startLoading();
final List<MVPModel> list = new ArrayList<>();
//the part that I trying to extract starts here.
Call call = DataRetriever.getDataByGet(URLCombiner.GET_FRONT_PAGE_ITEMS);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
mvpView.errorLoading();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
try {
JSONObject result = new JSONObject(response.body().string());
int errorCode = result.getInt("ErrorCode");
if (errorCode == 0) {
JSONArray value = result.getJSONObject("Value").getJSONArray("hot");
for (int i = 0; i < value.length(); i++) {
MVPModel mvpModel = new MVPModel();
String name = null;
String image = null;
try {
name = value.getJSONObject(i).getString("title");
image = URLCombiner.IP + value.getJSONObject(i).getString("pic");
} catch (JSONException e) {
e.printStackTrace();
}
mvpModel.setName(name);
mvpModel.setImage(image);
list.add(mvpModel);
}
if (list.size() > 0) {
mvpView.successLoading(list);
mvpView.finishLoading();
} else {
mvpView.errorLoading();
}
} else {
mvpView.errorLoading();
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
mvpView.errorLoading();
}
}
});
//the part that I trying to extract ends here.
}
}
당신은 내가 (I 데이터 관리자를 호출) 내가 할 수있는 희망하는 클래스에 libs와 OKHttp 사용하는 부분을 추출하기 위해 노력하고있어 볼 수 있듯이 서버와 클라이언트 사이의 모든 작업을 처리합니다. 하지만 여기에 데이터 관리자의 결과를 발표자에게 전달하려고 할 때 비동기 메커니즘 때문에 NullPointException이 발생합니다.
데이터 다운로드가 완료되면 비동기 서버에서 가져온 데이터를 발표자에게 전달하는 방법을 알고 싶습니다.
여기 내 이상적인 데이터 관리자가 있는데, 어리석은 것처럼 보일 수도 있지만 이것이 내 문제를 더 분명하게 만들 수 있다고 생각합니다.
public class LoadServerData {
private static JSONArray arrayData = new JSONArray();
public static JSONArray getServerData() {
Call call = DataRetriever.getDataByGet(URLCombiner.GET_FRONT_PAGE_ITEMS);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
try {
JSONObject result = new JSONObject(response.body().string());
int errorCode = result.getInt("ErrorCode");
if (errorCode == 0) {
arrayData = result.getJSONObject("Value").getJSONArray("hot"); //the data I would like to return.
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
return arrayData; //this is gonna be an empty data.
}
}
나는 내 문제를 해결할 수있는 몇 가지 기사를 읽고,하지만 여전히 어떤 좋은 대답을받지했습니다. 아마도 내가 생각한 잘못된 키워드를 사용했을 것입니다. 너희들이 나에게 도움이되거나 영감을 줄 수있는 아이디어 나 해결책을 줄 수 있기를 희망한다.
P. OKhttp libs와의 버전은 3.7.0
작동합니다. 도와 주셔서 감사합니다 : D – Lucian
당신을 환영합니다! 그러나'getServerData'가 정적 메소드가 아닌 ** 디자인을 변경하는 것을 고려하십시오. 이렇게하면 코드를 더 쉽게 테스트 할 수 있습니다. – Pelocho
통지 해 주셔서 다시 한 번 감사드립니다. 나는 그것을 바꿨다. 매우 고맙습니다. – Lucian