在这里,我试图发送json数据到我的and服务器,并使用php,我想解析json并将数据插入到mysql数据库中。我收到一个错误,说Value stdClass of type java.lang.String cannot be converted to JSONObject.
如何解决此错误?
private void insertToDb() throws JSONException {
billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
.getText().toString();
String invNumber = textViewInvNo.getText().toString();
String bcode = barCode.getText().toString();
String description = itemDesc.getText().toString();
String wt = weightLine.getText().toString();
String rateAmt = rateAmount.getText().toString();
String making = makingAmount.getText().toString();
String netr = netRate.getText().toString();
String iTotal=itemtotal.getText().toString();
String vatAmt =textViewVat.getText().toString();
String sumAmt =textViewSum.getText().toString();
String crtDate =textViewCurrentDate.getText().toString();
try {
jsonObject.put("custInfo", custSelected.toString());
jsonObject.put("invoiceNo", invNumber);
jsonObject.put("barcode", bcode);
jsonObject.put("desc", description);
jsonObject.put("weight",wt );
jsonObject.put("rate", rateAmt);
jsonObject.put("makingAmt", making);
jsonObject.put("net_rate",netr);
jsonObject.put("itemTotal",iTotal );
jsonObject.put("vat", vatAmt);
jsonObject.put("sum_total", sumAmt);
jsonObject.put("bill_type", billType);
jsonObject.put("date", crtDate);
} catch (JSONException e) {
e.printStackTrace();
}
try {
itemSelectedJson.put(index, jsonObject);
index++;
} catch (JSONException e) {
e.printStackTrace();
}
String jsonarray = itemSelectedJson.toString().trim();
JSONObject jb= new JSONObject();
jb.put("selected",itemSelectedJson);
Map<String,Object> params = new HashMap<>();
params.put("Array",jsonarray);
final JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, INVEST_URL, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("RESPONSE", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("JSONERROR",error.toString());
}
});
final RequestQueue queue = Volley.newRequestQueue(this);
queue.add(objectRequest);
}我是编程新手,所以任何帮助都是appreciated.Thank you:)
这是堆栈跟踪:
12-01 17:17:50.840 16785-16785/org.bordetuts.com.goldmine D/dalvikvm: GC_FOR_ALLOC freed 345K, 9% free 8597K/9396K, paused 14ms, total 14ms
12-01 17:17:50.841 16785-16785/org.bordetuts.com.goldmine D/SELECTED_ITEMS: [{"custInfo":"Ujwal 9975022560","rate":"24000","weight":"21.00000","desc":"GENTS ANGTHI 22k NO STONE","makingAmt":"200","sum_total":"RS.52094.46","vat":"RS.1021.46","itemTotal":"51073","barcode":"BQSP78BB","net_rate":"24200","date":"2015-12-01","invoiceNo":"1","bill_type":"Invoice"}]
12-01 17:17:50.897 16785-16785/org.bordetuts.com.goldmine W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-01 17:17:50.897 16785-16785/org.bordetuts.com.goldmine W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-01 17:17:51.259 16785-16785/org.bordetuts.com.goldmine D/JSONERROR: com.android.volley.ParseError: org.json.JSONException: Value stdClass of type java.lang.String cannot be converted to JSONObject
12-01 17:21:47.657 16785-16792/org.bordetuts.com.goldmine D/dalvikvm: GC_FOR_ALLOC freed 536K, 8% free 8689K/9396K, paused 171ms, total 190ms

这就是我在调试时看到的问题:当我说JSONObject( it.The )时,它会显示params: size=1 objectRequest:"[]
发布于 2015-12-01 20:33:47
在您的代码中,我认为您正在尝试将字符串stdClass转换为json对象,因此,如果您正在执行此过程,请遵循我给出的将字符串转换为json对象的示例
String json = {"phonetype":"N95","cat":"WP"};
try {
JSONObject obj = new JSONObject(json);
Log.d("My App", obj.toString());
} catch (Throwable t) {
Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
}发布于 2015-12-01 21:23:09
如果您遵循的是我的exmaple,那么请注意我使用了
Map<String,Object> 参数的插入方式如下:
Map<String, Object> params = new ArrayMap<>();
// Adding parameters to request
params.put("email", email); // here email is already a string,
params.put("password", password.toString()); // here password needs the conversion 然后我使用一个新的匿名JSONObject将它直接传递到一个请求中:
// Creating a JSON Object request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), ...这样,强制转换就会通过Object类。
还要注意,我的语法在请求之外和之前创建并放置这些参数。
编辑:你看到大小是1的原因是因为它实际上是1,这部分:
Map<String,Object> params = new HashMap<>();
params.put("Array",jsonarray);将映射大小设为1,因为您只将一个对象放入其中-键-值映射的数量是为映射大小给定的值,这意味着-指向某个值的键数(值本身可以是另一个映射,它仍然只能算作一个映射),请参见This Documentation以获取有关映射集合的完整解释。
关于其他部分-我建议尝试将您想要的值添加到映射中,并将其发送到新的JSONObject中,我回答了一些其他可能对您有帮助的Here。然后-在php端-打印出你得到的东西(看看你确实得到了它),一旦你在php端得到参数-你就可以开始解析它了。
希望这对您有任何帮助:)
发布于 2015-12-01 22:33:05
size是正确的1,因为您在地图中是这样放置的:
params.put("Array",jsonarray);因此json应该是1 object {"Array":[...]},您必须先访问此对象,然后才能解析数组。
https://stackoverflow.com/questions/34020180
复制相似问题