代码之家  ›  专栏  ›  技术社区  ›  Khalid Saeed

移动应用程序连接与Rest Api问题

  •  0
  • Khalid Saeed  · 技术社区  · 8 年前

    我有一个可以工作的rest api,但是当我尝试使用reformation发布数据时,它调用了reformation的faluire回调方法。我已经用postman测试了我的api以获取post请求,它在那里工作得很好,但是在android中它不工作。请帮忙:(。

    postman response image

    这是我的代码:

    产品.java

    public class Product {
        @SerializedName("name")
        private String name;
        @SerializedName("price")
        private Double price;
        @SerializedName("description")
        private String description;
        @SerializedName("category_id")
        private int category_id;
        private String message;
        public Product(String name,String price,String description,String category_id){
            this.name=name;
            this.price=Double.parseDouble(price);
            this.description=description;
            this.category_id=Integer.parseInt(category_id);
        }
    
        public String getMessage() {
            return message;
        }
    }
    

    package com.im_khalid.retrofitrestfulapi1.tutorial2;
    
    import retrofit2.Call;
    import retrofit2.http.Body;
    import retrofit2.http.POST;
    
    interface ProductClient {
    
        @POST("create.php")
        Call<Product> create(@Body Product product);
    }
    

    邮政数据.java

    public class postdata extends AppCompatActivity {
    
        EditText name,price,description,category_id;
        Button btn;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_postdata);
    
            name=findViewById(R.id.name);
            price=findViewById(R.id.price);
            description=findViewById(R.id.description);
            category_id=findViewById(R.id.category_id);
    
            btn=findViewById(R.id.button);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Product product=new Product(
                            name.getText().toString(),
                            price.getText().toString(),
                            description.getText().toString(),
                            category_id.getText().toString()
                    );
                    sendNetworkRequest(product);
                }
            });
    
    
        }
        public void sendNetworkRequest(Product product){
            Retrofit.Builder builder=new Retrofit.Builder()
                    .baseUrl("https://kk034538.000webhostapp.com/api/product/")
                    .addConverterFactory(GsonConverterFactory.create());
            Retrofit retrofit=builder.build();
            ProductClient client=retrofit.create(ProductClient.class);
            Call<Product> call=client.create(product);
            call.enqueue(new Callback<Product>() {
                @Override
                public void onResponse(Call<Product> call, Response<Product> response) {
                    Toast.makeText(postdata.this, "Message :"+ response.body().getMessage(), Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onFailure(Call<Product> call, Throwable t) {
                    Toast.makeText(postdata.this, "somthing went wrong :(", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    

    09-26 23:47:51.799 5014-5014/com.im_khalid.retrofitrestfulapi1 W/System.err: java.io.EOFException: End of input at line 1 column 1 path $
    09-26 23:47:51.800 5014-5014/com.im_khalid.retrofitrestfulapi1 W/System.err:     at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1401)
            at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
            at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
    09-26 23:47:51.801 5014-5014/com.im_khalid.retrofitrestfulapi1 W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
            at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
            at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:122)
            at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:217)
    09-26 23:47:51.802 5014-5014/com.im_khalid.retrofitrestfulapi1 W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:116)
            at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
            at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    09-26 23:47:51.803 5014-5014/com.im_khalid.retrofitrestfulapi1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
            at java.lang.Thread.run(Thread.java:764)
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Dhaval    8 年前
    public class Product
    {
       @SerializedName("name")
       private String name;
       @SerializedName("price")
       private Double price;
       @SerializedName("description")
       private String description;
       @SerializedName("category_id")
       private int category_id;
       private String message;
    
       public Product() {
    
       }
    
       public Product(String name,String price,String description,String category_id){
           this.name=name;
           this.price=Double.parseDouble(price);
           this.description=description;
           this.category_id=Integer.parseInt(category_id);
       }
    
       public String getName()
       {
          return name;
       }
    
       public void setName(String name)
       {
          this.name = name;
       }
    
       public Double getPrice()
       {
          return price;
       }
    
       public void setPrice(Double price)
       {
          this.price = price;
       }
    
       public String getDescription()
       {
          return description;
       }
    
       public void setDescription(String description)
       {
          this.description = description;
       }
    
       public int getCategory_id()
       {
          return category_id;
       }
    
       public void setCategory_id(int category_id)
       {
          this.category_id = category_id;
       }
    
       public String getMessage()
       {
          return message;
       }
    
       public void setMessage(String message)
       {
          this.message = message;
       }
    
    
    
    }
    

    请替换产品类的内容,然后重试。