带有Rest Api问题的移动应用程序连接

时间:2018-09-26 17:55:35

标签: android rest retrofit

我有一个可以正常工作的rest api,但是当我尝试使用改造发布数据时,它将调用改造的onfaluire回调方法。我已经用邮递员测试了我的api的发布请求,并且在那儿工作正常,但是在android中却无法正常工作。请帮助:(。

postman response image

这是我的代码:

Product.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;
    }
}

ProductClient.Java

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);
}

postdata.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();
            }
        });
    }
}

StackTrace:

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 个答案:

答案 0 :(得分:0)

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;
   }



}

替换Product类的内容,然后重试。