我使用gson从mysql中读取我的json。但是当我运行app时它返回值null。我不知道错误是什么,因为当我在本地使用相同的mysql运行app时,它正在工作。但是当我在主机中运行时,它返回null。请帮帮我!
{"group":
[{"category_id":"15","title":"Zing card - Zing xu"},
{"category_id":"14","title":"Th\u1ebb Gate - FPT"},
{"category_id":"20","title":"Vcoin - VTC - Audition"},
{"category_id":"21","title":"Garena"},
{"category_id":"32","title":"Mobay card"},
{"category_id":"33","title":"BIT Card"},
{"category_id":"34","title":"Oncash"}],
"product":
[
{"id":"39","group_id":"22","name":"Oncash","price":{"USD":9,"AUD":12.756909992913,"VND":195000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/oncach-e3bb9fa75d.gif","sku":"ONCASH200"},
{"id":"87","group_id":"33","name":"TH\u1eba BIT 5 TRI\u1ec6U","price":{"USD":252.42,"AUD":357.7888022679,"VND":4950000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/uwrki7jx-6198ed1975.gif","sku":"Bit5M"},
{"id":"88","group_id":"33","name":"TH\u1eba BIT 2 TRI\u1ec6U","price":{"USD":100.98,"AUD":143.13253012048,"VND":1980000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/91intahd-b42a80c01d.gif","sku":"Bit2M"},
{"id":"89","group_id":"33","name":"TH\u1eba BIT 1 TRI\u1ec6U","price":{"USD":50.48,"AUD":71.552090715804,"VND":990000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/nwd03knt-264a8d02fe.gif","sku":"Bit1M"},
{"id":"92","group_id":"33","name":"TH\u1eba BIT 500K","price":{"USD":25.23,"AUD":35.761871013466,"VND":495000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/8hxdajdl-9c8001602e.gif","sku":"Bit500K"},
{"id":"90","group_id":"33","name":"TH\u1eba BIT 200K","price":{"USD":10.08,"AUD":14.287739192062,"VND":198000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/ql3cn6iu-b4d8c3b407.gif","sku":"Bit200K"},
{"id":"91","group_id":"33","name":"TH\u1eba BIT 100K","price":{"USD":5.03,"AUD":7.1296952515946,"VND":100000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/oupebhyo-0a61ca65b2.gif","sku":"Bit100K"},
{"id":"93","group_id":"33","name":"TH\u1eba BIT 50K","price":{"USD":2.69,"AUD":3.8128986534373,"VND":50000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/zvznjqnz-52c80ccfe6.gif","sku":"Bit50K"},
{"id":"121","group_id":"33","name":"Th\u1ebb BIT 20k","price":{"USD":1.06,"AUD":1.5024805102764,"VND":20000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/the-bit-20k-6b4035c02d.gif","sku":"Bit20K"},
{"id":"80","group_id":"32","name":"TH\u1eba MOBAY 500K","price":{"USD":25.23,"AUD":35.761871013466,"VND":495000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/u5jywcz0-55285adfd7.gif","sku":"Mobay500K"},
{"id":"83","group_id":"32","name":"TH\u1eba MOBAY 200K","price":{"USD":10.08,"AUD":14.287739192062,"VND":198000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/db8f1s3y-170821fbe1.gif","sku":"Mobay200K"},
{"id":"82","group_id":"32","name":"TH\u1eba MOBAY 100K","price":{"USD":5.03,"AUD":7.1296952515946,"VND":100000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/57mzcwfk-40f4d60542.gif","sku":"Mobay100K"},
{"id":"85","group_id":"32","name":"TH\u1eba MOBAY 50K","price":{"USD":2.69,"AUD":3.8128986534373,"VND":50000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/hf8jilab-11ed516444.gif","sku":"Mobay50K"},
{"id":"84","group_id":"32","name":"TH\u1eba MOBAY 20K","price":{"USD":1.06,"AUD":1.5024805102764,"VND":20000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/pvrh3jey-dd0894e849.gif","sku":"Mobay20K"},
{"id":"53","group_id":"20","name":"Vcoin 200k","price":{"USD":10.08,"AUD":14.287739192062,"VND":198000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/v200-61d2763add.gif","sku":"VNS-VCOIN200K"},
{"id":"37","group_id":"20","name":"Vcoin 20k","price":{"USD":1.06,"AUD":1.5024805102764,"VND":20000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/v20-e0f120eefd.gif","sku":"vcoin20k"},
{"id":"118","group_id":"20","name":"Vcoin 1 tri\u1ec7u","price":{"USD":50.48,"AUD":71.552090715804,"VND":990000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/rpwzfkfy-655e715791.gif","sku":"VCOIN1TRIEU"},
{"id":"45","group_id":"14","name":"Gate 5 tri\u1ec7u","price":{"USD":252.42,"AUD":357.7888022679,"VND":4950000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/gate5trieu-c43aa69716.gif","sku":"GATE5000k"},
{"id":"44","group_id":"14","name":"Gate 2 tri\u1ec7u","price":{"USD":100.98,"AUD":143.13253012048,"VND":1980000},"images":"http:\/\/vnsupermark.com\/uploads\/catalog\/gate2trieu-091d584fce.gif","sku":"thegate2trieu"}
]}
函数return myjson
public ResListProduct getListProducts(Context context) {
Gson gson = new Gson();
ResListProduct resListProduct = null;
resListProduct = new ResListProduct();
try {
String urlApi = Store.getInstances(context).getA();
// URL
HashMap<String, String> params = new HashMap<String, String>();
int i = MainActivity.getVariable();
// all product
if (i == 1) {
params.put("command", "get_products");
}
if (i > 1) {
ArrayList<String> s = new ArrayList<String>();
s = MainActivity.getnamegrouptrem();
for (int j = 0; j < i - 1; j++) {
String[] titleActionbars = s.toArray(new String[s.size()]);
params.put("command", "get_product");
params.put("cagory", titleActionbars[j]);
}
// if (i == 9) {
// params.put("command", "get_product");
// params.put("cagory", String.valueOf(21));
// }// test autogen memu
}
InputStream stream = retrieveStream(urlApi, params, false, null,
null);
Reader reader = new InputStreamReader(stream);
//Printf JSON
// BufferedReader in = new BufferedReader(reader);
// String read;
// while ((read = in.readLine()) != null) {
// System.out.println("JSON" + read);
// }
// parse json to get list product
resListProduct = gson.fromJson(reader, ResListProduct.class);
} catch (Exception e) {
sendException(context,
"getListProduct: " + Log.getStackTraceString(e));
}
return resListProduct;
}
类对象
public class ResListProduct {
@SerializedName("product")
private List<Product> products;
@SerializedName("group")
private List<GroupProduct> groups;
public ResListProduct() {
}
public List<Product> getProducts() {
return products;
}
public List<GroupProduct> getGroups() {
return groups;
}
}
public class Product implements Serializable{
private static final long serialVersionUID = 6061052006970796100L;
@SerializedName("id")
private String idProduct;
@SerializedName("group_id")
private String groupId;
@SerializedName("name")
private String nameProduct;
@SerializedName("price")
private Price priceProduct;
@SerializedName("images")
private String urlImagesProduct;
@SerializedName("sku")
private String skuProduct;
@SerializedName("names")
private String names;
// quantity for each product
private int quantity;
// title group product
private String titleGroup;
public Product(String titleGroup) {
this.titleGroup = titleGroup;
}
public String getIdProduct() {
return idProduct;
}
public String getGroupId() {
return groupId;
}
public String getNameProduct() {
return nameProduct;
}
public Price getPriceProduct() {
return priceProduct;
}
public String getUrlImagesProduct() {
return urlImagesProduct;
}
public String getSkuProduct() {
return skuProduct;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getTitleGroup() {
return titleGroup;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
}
public class GroupProduct {
@SerializedName("category_id")
public String groupId;
@SerializedName("title")
public String name;
public String getName() {
return name;
}
public GroupProduct() {
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
}
答案 0 :(得分:0)
这里接受的答案对您有帮助吗?基本上它声明流被消耗了两次,因此读者对象中没有任何内容可以解析。 Parsing json directly using input stream