【发布时间】:2016-10-26 00:43:55
【问题描述】:
我正在尝试通过Asynctask 将JSON 对象传递给Web 服务。我在MainActivity 中调用了collectStatistics 方法。此方法收集移动设备的一些统计信息,并将它们组合在一个JSON 对象中。然后将JSON 对象传递给Asynctask。
JSONObject jsonProperties = new JSONObject();
try {
jsonProperties.put("_device" , _device);
jsonProperties.put("_model", _model);
jsonProperties.put("_product", _product);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonProperties.length() > 0)
{
//call to async class
//String.valueOf(jsonProperties)
//new SendJsonProperties().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, jsonProperties); //instead of execute
Toast.makeText(context,jsonProperties.toString(),Toast.LENGTH_LONG).show();
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new SendJsonProperties().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, jsonProperties);
Log.i("msg","calling asynctask");
} else {
new SendJsonProperties().execute(jsonProperties);
Log.i("msg","NOT calling asynctask");
}
}
Asynctask 接收 JSON 参数
class SendJsonProperties extends AsyncTask <JSONObject,String,String>
我在 doInBackground 中调用了一个 Web 服务调用者方法
protected String doInBackground(JSONObject... params)
{
//(...) elippsis make the input array
Log.i("msg", "do in bkgnd");
WebServicesCaller webServicesCaller = new WebServicesCaller();
result = webServicesCaller.storeStatistics(params[0]);
return null;
}
但我收到此错误
java.lang.RuntimeException: Cannot serialize: {"_product":"ms013gxx","_model":"SM-G7102","_device":"ms013g"}
我无法弄清楚序列化中的问题。任何帮助都感激不尽。提前致谢。
【问题讨论】:
标签: android json web-services android-asynctask