2017-10-09 26 views
-5

如何将数据的JSON对象类型内发送JSON阵列中发布请求到服务器在机器人如何将数据的JSON对象类型内发送JSON阵列中发布请求到服务器在机器人

{ 
    "user_id":"1", 
    "username":"shubham", 
    "order": [ 
       {"product_id":"2", 
       "qty":"5", 
       "price":"100", 
       "total":"500", 
       "product_name":"choclate" 
       }, 
       {"product_id":"1", 
       "qty":"2", 
       "price":"50", 
       "total":"500", 
       "product_name":"choclate" 
       } 
      ] 

} 
+0

看看这个:https://developer.android.com/training/volley/index.html – sumit

+0

可我知道你正在使用API​​调用的HTTP客户端? –

+1

询问之前如何搜索?搜索'android发送JSON post' –

回答

0
JSONObject obj=new JSONObject(); 
    obj.put("user_id",1); 
    obj.put("username","shubham"); 

    JSONArray array=new JSONArray(); 

    JSONObject objp=new JSONObject(); 
    // use for loop in case of multiple prodcuts 
    objp.put("product_id","2"); 
    objp.put("qty","5"); 
    objp.put("product_id","2"); 
    objp.put("price","100"); 
    objp.put("total","500"); 
    objp.put("product_name","choclate"); 

    array.put(objp.toString()); 
    obj.put("order",array.toString()) 

发送obj到服务器。

1
ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>(); 
    JSONObject postedJSON = new JSONObject(); 

    try { 
     objJSON.put("key", "value"); 
     // add key value for json formati 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    arrayList.add(postedJSON); 
    String variable = arrayList.toString(); 

发送字符串到服务器

了解REST API的POST Sending HTTP POST Request In Java参考职位:Ferrybig答案

0
-----------------------------------test.Order.java----------------------------------- 

package test; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Order { 

@SerializedName("product_id") 
@Expose 
private String productId; 
@SerializedName("qty") 
@Expose 
private String qty; 
@SerializedName("price") 
@Expose 
private String price; 
@SerializedName("total") 
@Expose 
private String total; 
@SerializedName("product_name") 
@Expose 
private String productName; 

public String getProductId() { 
return productId; 
} 

public void setProductId(String productId) { 
this.productId = productId; 
} 

public String getQty() { 
return qty; 
} 

public void setQty(String qty) { 
this.qty = qty; 
} 

public String getPrice() { 
return price; 
} 

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

public String getTotal() { 
return total; 
} 

public void setTotal(String total) { 
this.total = total; 
} 

public String getProductName() { 
return productName; 
} 

public void setProductName(String productName) { 
this.productName = productName; 
} 

} 
-----------------------------------test.TestClass.java----------------------------------- 

package test; 

import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class TestClass { 

@SerializedName("user_id") 
@Expose 
private String userId; 
@SerializedName("username") 
@Expose 
private String username; 
@SerializedName("order") 
@Expose 
private List<Order> order = null; 

public String getUserId() { 
return userId; 
} 

public void setUserId(String userId) { 
this.userId = userId; 
} 

public String getUsername() { 
return username; 
} 

public void setUsername(String username) { 
this.username = username; 
} 

public List<Order> getOrder() { 
return order; 
} 

public void setOrder(List<Order> order) { 
this.order = order; 
} 

} 

使用这两Class.Load数据这一类。并使用GSON Library。

Gson gson= new GSON(); 
String requiredJson= gson.toJson(testClassObject); 
相关问题