douqingzhi0980 2019-05-11 01:14
浏览 154

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

i am using rxjava and retrofit to get fetch data from server (localhost for testing) and then show it in recycler view. I have php script which i have tested by postman and its working fine but i could not be able to get this reponse using rxjava. The code i have written is follow.

php script 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);

The output it give as

{"login":[[{"q_contents":"abc","username":"abc"}]],"success":"0"}

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;
    }
}

In APiService Interface

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

Model class Post.class

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;
    }
}

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();
                }
            });
}

I am getting null in toast message that i have written in onNext().

  • 写回答

1条回答 默认 最新

  • dongxueji2838 2019-05-14 11:03
    关注

    The problem is that your json response does not correspond to your Post class.

    Instead you want to replace your APiService with:

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

    with PostResponse:

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

    and adapt the rest of your code accordingly.

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计