Android 将WebService与SQLite同步时,会出现数组和对象错误
我正在尝试将json数据输入sqlite,从web服务到android 这是我开始同步时出现的错误 错误出现在这段代码上Android 将WebService与SQLite同步时,会出现数组和对象错误,android,json,web-services,sqlite,Android,Json,Web Services,Sqlite,我正在尝试将json数据输入sqlite,从web服务到android 这是我开始同步时出现的错误 错误出现在这段代码上 public String processSync(String response) { String status = FAILED; try { String formattedData = getFormattedData(response); final GsonBuilder gsonBuilder = new G
public String processSync(String response) {
String status = FAILED;
try {
String formattedData = getFormattedData(response);
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
ProductDataBean[] productDataBeanArray = gson.fromJson(formattedData, ProductDataBean[].class);
AdministratorHelper administratorHelper = new AdministratorHelper();
status = administratorHelper.insertProductData(context, productDataBeanArray);
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
AdministratorHelper.java
public class AdministratorHelper {
DBLiteUtils dbLiteUtils;
AdministratorDao dao = new AdministratorDao();
public String insertProductData(Context context, ProductDataBean[] productDataBeanArray){
String status = FAILED;
try{
dbLiteUtils = new DBLiteUtils(context);
dbLiteUtils.open();
status = dao.insertProductData(dbLiteUtils, productDataBeanArray);
} catch (Exception e){
e.printStackTrace();
} finally {
dbLiteUtils.close();
}
return status;
}
AdministratorDao.java
public class AdministratorDao {
private Cursor cursor;
//Insert Product Details into SQLite
public String insertProductData(DBLiteUtils dbLiteUtils, ProductDataBean[] productDataBeanArray){
String status = FAILED;
try {
if(productDataBeanArray.length > 0) {
//Delete Existing Data
dbLiteUtils.deleteAll("ProductMaster");
//Base Insert Query for inserting New Data into ProductMaster Table
String query = "INSERT INTO ProductMaster(ItemId, Description, Barcode, TransStatusCode) VALUES ";
//Iterate the productDataBeanArray in order to get productDataBean which contains a single product details
for (ProductDataBean productDataBean : productDataBeanArray) {
//Concatenate the Base Insert Query and the product details
query = query+"('"+productDataBean.getItemId().trim()+"', '"+productDataBean.getDescription().trim().replace("\'", "\'\'")+"', '"+productDataBean.getBarcode().trim()+"', '"+productDataBean.getTransStatusCode().trim()+"'),"; //.trim() removes unwanted empty spaces
}
//Remove the last comma from the iterated complete query
query = query.substring(0, query.length()-1);
//execute the query
dbLiteUtils.executeQuery(query);
//return success
status = SUCCESS;
}
} catch (Exception e){
e.printStackTrace();
}
return status;
}
这是我在VisualStudio中获取json数据的代码
public class ProductController : ApiController
{
public IEnumerable<Product> Get()
{
List<Product> pproducts;
using (estocktakeEntities entities = new estocktakeEntities())
{
pproducts = entities.Products.ToList();
//return entities.products.ToList();
}
return pproducts;
}
错误日志是:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_对象,但在第1行列为字符串
,因此我猜问题是web服务提供的JSON正文
public String processSync(String response) {
String status = FAILED;
try {
String formattedData = getFormattedData(response);
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
ProductDataBean[] productDataBeanArray = gson.fromJson(formattedData, ProductDataBean[].class);
AdministratorHelper administratorHelper = new AdministratorHelper();
status = administratorHelper.insertProductData(context, productDataBeanArray);
} catch (Exception e) {
Log.e("tag",formattedData); // log out this JSON
e.printStackTrace();
}
return status;
}
而且edit1
告诉我这个问题可能发生在web服务中。(我猜web服务返回了错误的JSON)。我的建议是在Android项目中添加一些“容错”代码。
根本原因可能是web服务。您能用完整的JSON更新这个问题吗?这似乎是Gson错误。您能检查一下这一行吗?`ProductDataBean[]productDataBeanArray=gson.fromJson(formattedData,ProductDataBean[].class);`;尝试
ProductDataBean.class
而不是ProductDataBean[]。class
@PedroRodrigues嗨,先生,我的完整jason有4000多条记录。这是一样的,它将以}]结尾。@RustFisher嗨,先生,我尝试将其更改为productDataBean,但是我的“status=administratorHelper.insertProductData(context,productDataBeanArray);”将导致错误。我将更新并向您展示所有link@phan关于SQLite,我想ContentResolver
可能会有帮助:)谢谢你的建议,我会去查阅容错,同时用我的web服务更新这篇文章
public String processSync(String response) {
String status = FAILED;
try {
String formattedData = getFormattedData(response);
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
ProductDataBean[] productDataBeanArray = gson.fromJson(formattedData, ProductDataBean[].class);
AdministratorHelper administratorHelper = new AdministratorHelper();
status = administratorHelper.insertProductData(context, productDataBeanArray);
} catch (Exception e) {
Log.e("tag",formattedData); // log out this JSON
e.printStackTrace();
}
return status;
}