[英]Cannot read String response of Retrofit and RxJava POST request
Api Interface api接口
@FormUrlEncoded
@POST("register.php")
Observable<String> registerUser(@Field("email") String email, @Field("password") String password);
In my MVP presenter 在我的MVP主持人中
onCreate{
Observable<String> registerUserObservable=
apiInterface.registerUser("test@gmail.com", "1234");
registerUserObservable.subscribeOn("schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResult, this::handleError);
}
//methods
private void handleResult(String response){
Log.d(TAG, response);
}
private void handleError(Throwable throwable){
Log.d(TAG, throwable.getMessage());
}
These are my code for retrofit and rxjava and I am suppose to post an email and password to register a user. 这些是我用于改造和rxjava的代码,我想发布一个电子邮件和密码来注册用户。 The server should return a string on success and a string on failure too.
服务器应该在成功时返回一个字符串,在失败时也返回一个字符串。
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
I added the gson setLenient code portion because it gives me Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 1 path $
error if I don't have it. 我添加了gson setLenient代码部分,因为它使我可以
Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 1 path $
错误Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 1 path $
如果我没有)。 After adding this, I am getting JSON document was not fully consumed.
添加此内容后,我得到
JSON document was not fully consumed.
error which I do not know how to solve it. 错误,我不知道如何解决。 Is this because of the return response from the server is a string?
这是因为服务器返回的响应是字符串吗?
Thanks in advance. 提前致谢。
If your server sends normal string response not JSON string, you need to use another converter that reads string into String
. 如果您的服务器发送的是普通字符串响应而不是JSON字符串,则需要使用另一个将字符串读入
String
转换器。 Happily there is Scalars converter officially. 幸运的是,正式有Scalars转换器 。
A Converter which supports converting strings and both primitives and their boxed types to text/plain bodies.
一个Converter,它支持将字符串和基元及其装箱的类型都转换为文本/纯文本主体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.