저는 Android 및 반응 프로그래밍에 상당히 익숙하며 지난 이틀 동안 서버에서 게시물을 가져 와서 게시물에로드하는 활동을 시도했습니다 [ ] 내 애플 리케이션에서 나중에 사용됩니다. 여기서 문제는 그 Post []를 displayPostsMethod에 전달할 때 null이라는 것입니다. 여기에 활동RxAndroid 2.0.1 subscribe 메서드는 변수를 설정하지 않습니다.
public class HomeActivity extends AppCompatActivity{
private Post[] postsToDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//get the posts from the server
this.getPostsFromServer();
//dispaly the posts
this.displayPosts(posts);// here are the posts null
}
public void getPostsFromServer() {
PostsProvider.getAllPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(posts -> {
this.postsToDispaly = Arrays.copyOf(posts, posts.length);
});
}
}
의 코드입니다 그리고 여기 또한 getAllPostsMethod PostsProvider에서 클래스의 코드입니다.
public static Observable<Post[]> getAllPosts(){
return Observable.create((ObservableEmitter<Post[]> e) -> {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("sample url")
.build();
Response response = client.newCall(request).execute();
String json = response.body().string();
Gson gson = new Gson();
Post[] posts = gson.fromJson(json, Post[].class);
e.onNext(posts);
});
}
json을 넣을 수 있습니까? –
나는 그것이 json에서 나온 것이라고 생각하지 않는다. 왜냐하면 나는 okhttp를 사용하지 않기 때문에 getAllPosts()에서 POST []를 하드 코딩하고 emitt도 똑같이한다. –
그래서 this.displayPosts (posts);를'this.postsToDispaly = Arrays.copyOf (posts, posts.length); 아래로 이동 시키려고합니까? –