我正在使用rxjava和改造来从服务器(用于测试的本地主机)获取数据,然后在回收器视图中显示它。我有由邮递员测试过的php脚本,它的工作正常,但我无法使用rxjava得到此响应。我写的代码如下。
php脚本 getPosts.php
AttributeError: 'TextItem' object has no attribute 'text'
输出为
<?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
{"login":[[{"q_contents":"abc","username":"abc"}]],"success":"0"}
在 APiService 界面
中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;
}
}
模型类 Post.class
@GET("getPosts.php")
Observable<Post> getPost();
RxJava
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;
}
}
我在onNext()中编写的吐司消息中得到了空值。
答案 0 :(得分:0)
问题是您的json响应与您的Post
类不对应。
相反,您想将APiService
替换为:
@GET("getPosts.php")
Observable<PostResponse> getPost();
与PostResponse
:
class PostResponse {
List<List<Post>> login;
String success;
}
并相应地修改其余代码。