gpt4 book ai didi

android - MalformedJsonException : Use JsonReader. setLenient(true) 在第 1 行第 1 列路径中接受格式错误的 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:11 25 4
gpt4 key购买 nike

尝试使用 Retrofit 发送 JSON 格式的信息,但它进入 Retrofit 的 onFailure 方法并抛出以下错误:

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

到目前为止,我已经尝试使用以下链接中的答案来解决它:1)MalformedJsonException with Retrofit API?2) Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

这是我的代码:

retrofit 界面:

public interface ServerApi {
@POST("/register/test")
Call<User> createAccount(@Body User user);
}

带有连接内容的 MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

User user= new User("myemail@mail.ru","vercode100");
sendNetworkRequest(user);
}

private void sendNetworkRequest(User user){

//create Retrofit instance
Retrofit.Builder builder= new Retrofit.Builder()
.baseUrl("http://localhost:8080")
.addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit= builder.build();

//Get client and call object for the request
ServerApi client= retrofit.create(ServerApi.class);
Call<User> call= client.createAccount(user);

call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
}

@Override
public void onFailure(Call<User> call, Throwable t) {
}
});

}
}

用户类:

public class User {
private String email;
private String verificationCode;

public User(String email, String verificationCode) {
this.email = email;
this.verificationCode = verificationCode;
}

}

服务器端需要这样的 JSON:

{
"email" : "user.email",
"verificationCode" : "123456"
}

我知道stackoverflow有一些常见的问题,但是都没有完全解决我的问题。

最佳答案

GsonBuilder gsonBuilder = new GsonBuilder();  
gsonBuilder.setLenient();
Gson gson = gsonBuilder.create();

// and in you adapter set this instance
GsonConverterFactory.create(gson)

关于android - MalformedJsonException : Use JsonReader. setLenient(true) 在第 1 行第 1 列路径中接受格式错误的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com