【问题标题】:Why does my retrofit code not insert data in the database?为什么我的改造代码没有在数据库中插入数据?
【发布时间】:2019-11-23 01:19:01
【问题描述】:

为什么我的改造代码不能在数据库中插入数据?我在 iis 上发布了我的 wcf 服务,当我在浏览器中传递参数时它工作正常。

这是我的界面代码:

interface ApiInterface {

@POST("client")
Call<client>insert(@Body client cl);
}

这是我的模型类:

 public client(String name, String contact, String addres) {
    this.name = name;
    this.contact = contact;
    this.addres = addres;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

这是主要活动:

    textname = (EditText) findViewById(R.id.name);
    textcontact = (EditText) findViewById(R.id.contact);
    textpssword = (EditText) findViewById(R.id.password);
    Button btn = findViewById(R.id.send);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            client cl=new client(textname.getText().toString(),

textcontact.getText().toString(),textpssword.getText().toString());
            sendNetworkrequest(cl);
        }
    });

}

private void sendNetworkrequest(client cl){

    Retrofit.Builder builder=new 


 Retrofit.Builder().baseUrl("http://192.168.1.20/Service1.svc/").
 addConverterFactory(GsonConverterFactory.create());
    Retrofit retrofit=builder.build();

    ApiInterface apiInterface=retrofit.create(ApiInterface.class);
    Call<client> call=apiInterface.insert(cl);
    call.enqueue(new Callback<client>() {
        @Override
        public void onResponse(Call<client> call, Response<client> 
 response) {
            Toast.makeText(MainActivity.this,"compl 
 inserted",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call<client> call, Throwable t) {
            Toast.makeText(MainActivity.this, "some thing went wrng", 
Toast.LENGTH_SHORT).show();
        }
    });

我可以通过baseurl的写入格式吗?

这在浏览器中运行良好

http://192.168.1.20:8080/Service1.svc/insert?name=mehmood&contact=123&addres=lahore 

是本地发布的地址。 insert 是 wcf 服务中定义的函数。

【问题讨论】:

  • 您的插入方法在 WCF 中定义为使用 http-get,通常使用 [webget] 属性进行修饰。所以我们在构造HTTP请求时,应该使用查询字符串而不是请求体来传递参数(实体的属性)
  • 完全正确.... [OperationContract] [WebInvoke (Method="GET",ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Json,UriTemplate="/insert?name ={name}&contact={cont}&addres={addr}")] string insert(string name, string cont, string addr);那我做什么
  • 如果构造的http请求是POST并附加一个JSON请求体,我们可以像下面这样定义OperationContract, [OperationContract] [WebInvoke(Method ="POST",RequestFormat =WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Bare)] String InSertProduct(Product p); [DataContract] public class Product { [DataMember] public int ID { get;放; } [DataMember] 公共字符串名称 { 获取;放; } }

标签: android wcf retrofit2


【解决方案1】:

您的示例请求 (http://192.168.1.20:8080/Service1.svc/insert?name=mehmood&contact=123&addres=lahore) 正在使用查询参数发送字段。但是您的改造定义似乎将您的参数放入请求正文中。

如果您的服务在 POST 请求中接受查询参数,那么您需要执行以下操作:

@POST("/insert")
Call<client> insert( 
    @Query("name") String name, 
    @Query("contact") String contact, 
    @Query("addres") String addres
);

发布带有查询参数的东西似乎很奇怪,但也许你有充分的理由 4。

顺便说一句,您实际上会将您的客户端对象作为返回类型,这不是请求类型。

请将客户端重命名为客户端。我太书呆子了,不提它;)

【讨论】:

  • 感谢您的专注,先生....我是 android 新手..但是这部分有点混乱。在呼叫我做什么。抱歉英语不好,因为它不是我的母语 ApiInterface apiInterface=retrofit.create(ApiInterface.class);调用 call=apiInterface.insert(cl);
  • 您应该将 ApiInterface 中的插入函数更改为 KnechtRootrecht 建议的函数
  • 这意味着在调用函数时必须使用查询参数而不是改造主体参数。
  • ApiInterface apiInterface=retrofit.create(ApiInterface.class);客户 c=新客户(姓名、联系人、地址);调用 call=apiInterface.insert(c.getName(),c.getcontact(),c.getaddres());它不起作用
猜你喜欢
  • 2016-02-21
  • 2019-05-03
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多