Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用rxjava和改型从服务器获取响应_Php_Retrofit_Rx Java - Fatal编程技术网

Php 使用rxjava和改型从服务器获取响应

Php 使用rxjava和改型从服务器获取响应,php,retrofit,rx-java,Php,Retrofit,Rx Java,我正在使用rxjava和改型从服务器(用于测试的本地主机)获取数据,然后在recycler视图中显示它。我有一个php脚本,我已经由postman测试过了,它工作得很好,但是我无法使用rxjava获得这个响应。我写的代码如下 php脚本getPosts.php <?php require_once "DbOperations.php"; $response = array(); $response["login"] = array(); $db = new DbOperations();

我正在使用rxjava和改型从服务器(用于测试的本地主机)获取数据,然后在recycler视图中显示它。我有一个php脚本,我已经由postman测试过了,它工作得很好,但是我无法使用rxjava获得这个响应。我写的代码如下

php脚本getPosts.php

<?php
require_once "DbOperations.php";
$response = array();
$response["login"] = array();
$db = new DbOperations();
$user = $db->getPosts();
$response["success"] = "0";
array_push($response["login"],$user);   
echo json_encode($response);
ApiClient.class

public class ApiClient {

    public static final String BASE_URL = "http://192.168.43.133:8080/ProblemSolver/includes/";
    private static Retrofit retrofit = null;
    public static Retrofit getClient() {

        if (retrofit==null) {
            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }

        return retrofit;
    }
}
public class Post {
    private String q_contents;
    private String username;

    public Post() {
    }

    public Post(String q_contents, String username) {
        this.q_contents = q_contents;
        this.username = username;
    }

    public String getQ_contents() {
        return q_contents;
    }

    public void setQ_contents(String q_contents) {
        this.q_contents = q_contents;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
在APiService界面中

@GET("getPosts.php")
Observable<Post> getPost();
RxJava

private void loadRecentPosts() {
    Retrofit retrofit = ApiClient.getClient();
    mService = retrofit.create(APIService.class);
    mService.getPost()
            .subscribeOn(Schedulers.io())

            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Post>() {
                @Override
                public void onSubscribe(Disposable d) {
                }

                @Override
                public void onNext(Post post) {
                    Toast.makeText(getContext(),""+post.getUsername(),Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(Throwable e) {
                    Log.d("", "onError: "+e.getMessage());
                }

                @Override
                public void onComplete() {
                    Toast.makeText(getContext(),"complete",Toast.LENGTH_LONG).show();
                }
            });
}
private void loadRecentPosts(){
改装改装=ApiClient.getClient();
mService=reformation.create(APIService.class);
mService.getPost()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(新观察员){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
下一页(邮政)的公共作废{
Toast.makeText(getContext(),“”+post.getUsername(),Toast.LENGTH_LONG.show();
}
@凌驾
公共无效申报人(可丢弃的e){
Log.d(“,”onError:“+e.getMessage());
}
@凌驾
未完成的公共空间(){
Toast.makeText(getContext(),“complete”,Toast.LENGTH_LONG.show();
}
});
}

我在onNext()中编写的toast消息中出现null。

问题是json响应与
Post
类不对应

相反,您希望将您的
APiService
替换为:

@GET("getPosts.php")
Observable<PostResponse> getPost();

并相应地调整其余代码。

问题在于json响应与
Post
类不对应

相反,您希望将您的
APiService
替换为:

@GET("getPosts.php")
Observable<PostResponse> getPost();
并相应地调整代码的其余部分

class PostResponse {
    List<List<Post>> login;
    String success;
}