如何使用Volley库将对象从Mobile应用程序发送到PHP Web服务

时间:2016-07-08 09:52:02

标签: java php android oop android-volley

有一个Product类。形成产品类的对象列表,该列表封装在Order类的对象中。这两个课程都在下面给出。

Product.java

package com.example.gandhjee.pantry_order;

import java.io.Serializable;

public class Product implements Serializable{
    private int id;
    private String name;
    private int amount;
    private int final_id;
    private int price;
    private int price_per_plate;
    //private String description;
    private static final long serialVersionUID = -5435670920302756945L;

    //Constructor

    public Product(int final_id,int id, String name, int amount , int price ,int price_per_plate) {
        this.id = id;
        this.name = name;
        this.amount = amount;
        this.setName(name);
        this.setAmount(amount);
        this.setFinal_id(final_id);
        this.price_per_plate = price_per_plate;
        this.final_id = final_id;
        this.price = price;
    }

    public int getPrice_per_plate(){
        return price_per_plate;
    }

    public int getPrice(){
        return price;
    }

    public void setPrice(int price){
        this.price = price;
    }

    public void setFinal_id(int final_id){
        this.final_id=final_id;
    }

    public int getFinal_id(){
        return final_id;
    }

    public int getId(){
        return id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getName(){
        return name;
    }

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

    public int getAmount(){
        return amount;
    }

    public void setAmount(int amount){
        this.amount = amount;
    }


}

Order.java

package com.example.gandhjee.pantry_order;
import java.io.Serializable;
import java.util.List;

public class Order implements Serializable{
    private int order_id;
    private List<Product> mProductList;
    private String emp_name;
    private int area_id;
    private int emp_id;
    private int conf_room_id;
    private int type_id;

    //private String description;
    private static final long serialVersionUID = -5435670920302756945L;

    public Order(List<Product> mProductList, int order_id,int emp_id, String emp_name, int type_id , int area_id ,int conf_room_id ){
        this.mProductList = mProductList;
        this.order_id = order_id;
        this.emp_name = emp_name;
        this.emp_id = emp_id;
        this.setType_id(type_id);
        this.setconf_room_id(conf_room_id);
        this.setArea_id(area_id);
    }

    public void setmProductList(List<Product> mProductList){
    this.mProductList = mProductList;
}
    public Product getmProductList(){
        return (Product) mProductList;
    }

    public int getConf_room_id(){
        return conf_room_id;
    }

    public void setconf_room_id(int conf_room_id){
        this.conf_room_id = conf_room_id;
    }

    public int getArea_id(){
        return area_id;
    }

    public void setArea_id(int area_id){
        this.area_id = area_id;
    }

    public void setType_id(int type_id){
        this.type_id=type_id;
    }

    public int getType_id(){
        return type_id;
    }

    public int getOrder_id(){
        return order_id;
    }

    public String getEmp_name(){
        return emp_name;
    }

    public void setEmp_name(String emp_name){
        this.emp_name = emp_name;
    }

    public int getEmp_id(){
        return emp_id;
    }

    public void setEmp_id(int emp_id){
        this.emp_id = emp_id;
    }

}

现在我想使用Volley库从我的移动应用程序向PHP中的Web服务发送一个Object of Order类。我在互联网上搜索了很多,但没有给出将对象从移动应用程序发送到Web服务的示例。任何帮助,将不胜感激 。提前谢谢!

1 个答案:

答案 0 :(得分:1)

创建一个JSONObject并将其发送到服务器。

你可以手动使用net.json或(我建议你这样做)使用Gson。

在这里,您可以找到实现它的教程供您使用: https://guides.codepath.com/android/Leveraging-the-Gson-Library