将 Web 服务与 SQLite 同步时,会出现数组和对象错误



我正在尝试将我的json数据从Web服务到Android的SQLite中。

这是我开始同步时遇到的错误。

错误指出预期BEGIN_ARRAY但在第 1 行第 2 列 2 路径 $ 处BEGIN_OBJECT

错误出现在此代码块上。

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;
}

管理员助手.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;
}

管理员道.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;
}

这是我在视觉工作室中获取 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;
}

这是我的json示例

[{"invtid":"02007997 ","ib_itemcode1":"0 ","transstatuscode":"IN","descr":"潘婷C/C重症监护口罩6 x 150m"}, {"invtid":"1","ib_itemcode1":"1","transstatuscode":"IN","descr":"object1"},{"invtid":"13101336","ib_itemcode1":"47400179172 ","transstatuscode":"IN","descr":"吉列马赫 3 分配器 8S (X12) "}, {"invtid":"13101473","ib_itemcode1":"47400179349","transstatuscode":"IN","descr":"吉列马赫3推车4S(X12)

如果需要更多信息,请告诉我,我将非常愿意上传它们。

谢谢先生的时间。

编辑 1

尝试调试后,我遇到了此错误,仍然不确定它是否有用,但这是错误和与之相关的代码。

{"消息":">

发生错误。","异常消息":"值不能为空。\r参数名称: 实体","异常类型":"System.ArgumentNullException","StackTrace":" at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)\r at System.Data.Entity.DbSet'1.Add(TEntity entity)\r at ProductServiceFinal.Controllers.ProductController.Post(Product productrecord)"}

ProductController.Post

public HttpResponseMessage Post([FromBody] Product productrecord)
{
try
{
using (estocktakeEntities entities = new estocktakeEntities())
{
entities.Products.Add(productrecord);
//entities.Entry(productrecord).State = System.Data.Entity.EntityState.Modified;
entities.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created, productrecord);
message.Headers.Location = new Uri(Request.RequestUri + productrecord.invtid.ToString());
return message;
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}

错误日志是:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column所以我想问题是提供的 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;
}

EDIT 1告诉我,问题可能发生在Web服务中。(我猜网络服务返回了一个错误的 JSON)。
我的建议是向Android项目添加一些"容错"代码。
根本原因可能出在 Web 服务中。

相关内容

  • 没有找到相关文章

最新更新