gpt4 book ai didi

java - 检索太多行后,Asynctask 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:04 27 4
gpt4 key购买 nike

我正在做一个应用程序,每次用户登录时,我都会将在线数据库同步到离线数据库。该表被离线删除,重新创建,然后添加新行(有必要删除它并添加新行,而不是仅仅检查和添加表中尚未存在的行)。我的在线表中有大约 200 行,它们相对较快地同步到我的离线表(在后台,然后我尝试了 3000 行,它仍在处理中。但是当我生成 90 000 行并尝试将其同步到我的离线数据库时,它不会移动。

onPreExecute() 中的日志已执行,但我的 doInBackground 中没有任何日志。 json 不为空。对于每个检索到的行,我都会离线添加一行。

有人知道可能是什么问题吗?我尝试在我的 PHP 脚本中添加 LIMIT 200 但仍然没有这样做,这很奇怪,因为当我有 200 行时它会执行,但当我将输出限制为 200 时它不会执行。感谢您的任何回答,这将使我更接近解决方案。

public class SyncVykresToOffline {

String DataParseUrl = "/scriptsforandroidapplicationofflinemode/SyncVykresToOffline.php";
JSONObject json = null;
String str = "";
HttpResponse response;
DBHelper dbh;
private Context mContext;
public static boolean syncedvykres = false;
int k = 200;

public SyncVykresToOffline(Context context) {
mContext = context;
dbh = new DBHelper(mContext);
}


public class SyncVykres extends AsyncTask<Void, Void, Void>
{
public Context context;


public SyncVykres(Context context)
{
this.context = context;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
Log.i("Poradie_zacal","ano");
}

@Override
protected Void doInBackground(Void... arg0)
{




HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost(DataParseUrl);


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("limit", String.valueOf(k)));
try {
myConnection.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}

try {
HttpResponse response = myClient.execute(myConnection);
} catch (IOException e1) {
e1.printStackTrace();
}


try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

int i = 0;
try{

int vykres_version;

JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(i);
Log.i("Poradie_json",String.valueOf(jArray.length()));

String
Nazov_vykresu;


int Version,
ID_vykres,
ID_stav,
ID_zakazka,
Poradie;

if(json == null) {
Log.i("Poradie","son is null");
}


while(json != null) {
Log.i("Poradie","been here");
ID_vykres = Integer.parseInt(json.getString("ID_vykres"));
vykres_version = dbh.getVykresVersion(ID_vykres);
Nazov_vykresu = json.getString("Nazov_vykresu");
ID_stav = Integer.parseInt(json.getString("ID_stav"));

ID_zakazka = Integer.parseInt(json.getString("ID_zakazka"));
Version = Integer.parseInt(json.getString("Version"));
Poradie = Integer.parseInt(json.getString("Poradie"));
Log.i("Poradie",json.getString("Poradie"));

dbh.SyncVykresToOffline(new technicky_vykres(ID_vykres,Nazov_vykresu,ID_stav,ID_zakazka,Version,Poradie));

i++;
json = jArray.getJSONObject(i);
}



} catch ( JSONException e) {
e.printStackTrace();
}

catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result)
{
syncedvykres = true;
}
}
}

编辑:添加 Logcat 日志。

06-25 20:36:07.013 8278-8308/com.example.chris.normitapplication W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.example.chris.normitapplication.offline.SyncVykresToOffline$SyncVykres.doInBackground(SyncVykresToOffline.java:102)
at com.example.chris.normitapplication.offline.SyncVykresToOffline$SyncVykres.doInBackground(SyncVykresToOffline.java:44)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)

编辑 2:添加了从中检索 JSON 数组的 PHP 脚本:

<?php

include 'DatabaseConfig.php';

$conn = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


mysqli_set_charset($conn,"utf8");


$vykres = array();



$sql = "SELECT * FROM `technicky_vykres`";

$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));



while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}


echo json_encode($emparray);

$conn->close();
?>

记录 STR 时发现的问题:

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>openresty</center>
</body>
</html>

最佳答案

我的直觉是,它的数据打破了循环并导致程序在没有遍历完整数据集的情况下结束。我只是添加了简单的 try/catch (见下文)来打印任何数据对象,我们无法解析这些数据对象,但仍然继续处理下一行。当然,您需要为生产质量代码提供更好的错误处理。

     while(json != null) {
try{
Log.i("Poradie","been here");
ID_vykres = Integer.parseInt(json.getString("ID_vykres"));
vykres_version = dbh.getVykresVersion(ID_vykres);
Nazov_vykresu = json.getString("Nazov_vykresu");
ID_stav = Integer.parseInt(json.getString("ID_stav"));

ID_zakazka = Integer.parseInt(json.getString("ID_zakazka"));
Version = Integer.parseInt(json.getString("Version"));
Poradie = Integer.parseInt(json.getString("Poradie"));
Log.i("Poradie",json.getString("Poradie"));

dbh.SyncVykresToOffline(new technicky_vykres(ID_vykres,Nazov_vykresu,ID_stav,ID_zakazka,Version,Poradie));

i++;
json = jArray.getJSONObject(i);
}catch(Exception e){
//Something wrong with the data? log it and see if you can find the culprit row/data....
Log.i("The faulty jason obj: " + json);
continue; //Move on the next one....
}
}

关于java - 检索太多行后,Asynctask 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com