我正在使用翻新功能将呼叫发送到api,以通过我的android应用程序将问题发布到bug跟踪器中。 API需要以特定的方式对其进行格式化,因此我创建了一个自定义对象以作为参数传递。不幸的是,当我将请求发送到API时,其格式错误。它按字母顺序排列参数,在键和值两边加上引号,并在数据的开头添加@,导致API无法理解我要发布的内容。我要发送的格式是:
{ fb_user_id: dummyUID,
email: dummy@email.com,
first_name: John,
last_name: Smith,
project: android,
type: bug,
subject: title,
description: description }
代替上面的内容,我的api接收的数据为
@"description":"description",
"email":"dummy@email.com",
"fb_user_id":"dummyUID",
"first_name":"John",
"last_name":"Smith",
"project":"android",
"subject":"title",
"type":"bug"
我正在使用改装,界面看起来像
public interface MyAPI {
@POST("/op_create_ticket")
Observable<JsonElement> createTicket(@Body Ticket ticket);
}
我的活动称之为:
public class HelpActivity extends BaseActivity {
@BindView(R.id.textView)
TextView textView;
@BindView(R.id.spinner)
Spinner spinner;
@BindView(R.id.email)
EditText email;
@BindView(R.id.firstName)
EditText firstName;
@BindView(R.id.lastName)
EditText lastName;
@BindView(R.id.subject)
EditText title;
@BindView(R.id.description)
EditText description;
@BindView(R.id.button3)
Button button3;
FirebaseAuth fbauth = FirebaseAuth.getInstance();
FirebaseUser user = fbauth.getCurrentUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
ButterKnife.bind(this);
}
@OnClick(R.id.button3)
public void onViewClicked() {
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
Retrofit.Builder builder = new Retrofit.Builder();
builder.baseUrl("https://my.url");
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
builder.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
MyAPI api = retrofit.create(TriggerWatchAPI.class);
Ticket ticket = new Ticket();
ticket.setFb_user_id(user.getUid());
ticket.setEmail(email.getText().toString());
ticket.setFirst_name(firstName.getText().toString());
ticket.setLast_name(lastName.getText().toString());
ticket.setProject("android");
ticket.setType("design");
ticket.setSubject(title.getText().toString());
ticket.setDescription(description.getText().toString());
api.createTicket(ticket).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonElement>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(JsonElement jsonElement) {
Log.d(getTag(), "zzzOnNext");
}
@Override
public void onError(Throwable e) {
Log.e(getTag(), "err", e);
}
@Override
public void onComplete() {
}
});
}
}
我想知道是否有某种转换器或某种可以将我的对象转换为API正在寻找的数据的类型。
编辑:忘记添加我的自定义pojo
package watch.trigger.Model;
public class Ticket {
private String project;
private String first_name;
private String email;
private String description;
private String subject;
private String last_name;
private String fb_user_id;
private String type;
public String getProject ()
{
return project;
}
public void setProject (String project)
{
this.project = project;
}
public String getFirst_name ()
{
return first_name;
}
public void setFirst_name (String first_name)
{
this.first_name = first_name;
}
public String getEmail ()
{
return email;
}
public void setEmail (String email)
{
this.email = email;
}
public String getDescription ()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public String getSubject ()
{
return subject;
}
public void setSubject (String subject)
{
this.subject = subject;
}
public String getLast_name ()
{
return last_name;
}
public void setLast_name (String last_name)
{
this.last_name = last_name;
}
public String getFb_user_id ()
{
return fb_user_id;
}
public void setFb_user_id (String fb_user_id)
{
this.fb_user_id = fb_user_id;
}
public String getType ()
{
return type;
}
public void setType (String type)
{
this.type = type;
}
@Override
public String toString()
{
return "ClassPojo [project = "+project+", first_name = "+first_name+", email = "+email+", description = "+description+", subject = "+subject+", last_name = "+last_name+", fb_user_id = "+fb_user_id+", type = "+type+"]";
}
}
答案 0 :(得分:0)
不发送自定义对象,不发送JSON对象作为字符串,而在API中将其作为JSON对象接收。
首先将其添加到您的gradle中:
public interface MyAPI {
@Headers("Content-Type: application/json")
@POST("/op_create_ticket")
Observable<JsonElement> createTicket(@Body Ticket ticket);
}
1-更改
public interface MyAPI {
@POST("/op_create_ticket")
Observable<String> createTicket(@Body String ticket);
}
至:
Ticket ticket = new Ticket();
ticket.setFb_user_id(user.getUid());
ticket.setEmail(email.getText().toString());
ticket.setFirst_name(firstName.getText().toString());
ticket.setLast_name(lastName.getText().toString());
ticket.setProject("android");
ticket.setType("design");
ticket.setSubject(title.getText().toString());
ticket.setDescription(description.getText().toString());
api.createTicket(ticket).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonElement>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(JsonElement jsonElement) {
Log.d(getTag(), "zzzOnNext");
}
@Override
public void onError(Throwable e) {
Log.e(getTag(), "err", e);
}
@Override
public void onComplete() {
}
});
2-更改此
JSONObject tickenJson= new JSONObject();
Ticket ticket = new Ticket();
ticket.setFb_user_id(user.getUid());
ticket.setEmail(email.getText().toString());
ticket.setFirst_name(firstName.getText().toString());
ticket.setLast_name(lastName.getText().toString());
ticket.setProject("android");
ticket.setType("design");
ticket.setSubject(title.getText().toString());
ticket.setDescription(description.getText().toString());
ticketJson.put("fb_user_id",user.getUid());
ticketJson.put("email",email.getText().toString());
ticketJson.put("first_name",firstName.getText().toString());
ticketJson.put("last_name",lastName.getText().toString());
ticketJson.put("project","android");
ticketJson.put("type","design");
ticketJson.put("type",title.getText().toString());
ticketJson.put("description",description.getText().toString());
api.createTicket(ticketJson.toString()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonElement>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(JsonElement jsonElement) {
Log.d(getTag(), "zzzOnNext");
}
@Override
public void onError(Throwable e) {
Log.e(getTag(), "err", e);
}
@Override
public void onComplete() {
}
});
到
{{1}}