У меня есть проблема, которую я не сохраненная в таблице, если я получаю его, но он дает мне ошибку в момент трансформации Jsonсинхронизации с Mysql Sqlite
Спасибо за помощь заранее. (Я новичок в этом)
Метод, который отправляет данные на сервер
классpublic void sync(View v) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in...");
pDialog.show();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_SYNC, new Response.Listener<String>() {
public void onResponse(String response) {
Log.d("Login Response",
"Login Response: " + response.toString());
pDialog.cancel();
String user = config.getUser();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
int contLogin = objSqlt.getUser(user);
if (contLogin == 0) {
JSONObject obj = jObj.getJSONObject("user");
Integer id_mysql = obj.getInt("id_mysql");
String user1 = obj.getString("user");
String pass_encrypted = obj.getString("pass_encrypted");
String pass1 = obj.getString("pass");
// Inserting row in users table
objSqlt.addUser(id_mysql, user1, pass_encrypted, pass1);
}
} else {
session.setControl_login(false);
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.e("Sysn Error: ", error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
pDialog.cancel();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = objSqlt.getJSONfromSQLite();
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
Моя функция SQLite, который запрашивает базу данных андроида
public Map<String, String> getJSONfromSQLite(){
Map<String, String> array = new HashMap<String, String>();
Map<String, String> json = new HashMap<String, String>();
SQLiteDatabase database = this.getWritableDatabase();
int control=0;
String selectQuery = "SELECT id_sqlite, user, company, form, checkBox, date FROM chequeo where status = '2' ";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_sqlite", cursor.getString(0));
map.put("user", cursor.getString(1));
map.put("company", cursor.getString(2));
map.put("form", cursor.getString(3));
map.put("checkBox", cursor.getString(4));
map.put("date", cursor.getString(5));
array.put(control+"",map+"");
control++;
} while (cursor.moveToNext());
}
database.close();
Log.d("map", array.toString());
json.put("json", array.toString());
return json;
}
ТЕКУЩ показывает мне
04-14 14:07:07.938: D/Login Response(1038): Login Response:
04-14 14:07:07.938: D/Login Response(1038): string(819)
"{3={date=2015-04-10, checkBox=[[0], [1], [0], [0], [0], [0], [0], [0],
[0], [0], [0], [0]], form=2, user=a, id_sqlite=4, company=2},
2={date=2015-04-10 20:57:59, checkBox=[[1], [1], [0], [0], [0], [0],
[0], [0], [0], [0], [0], [0]], form=3, user=a, id_sqlite=3, company=1},
1={date=2015-04-10 20:50:05, checkBox=[[0], [0], [0], [0], [1], [0],
[0], [0], [0], [0], [0], [0]], form=1, user=a, id_sqlite=2, company=1},
0={date=2015-04-10 20:33:17, checkBox=[[0], [1], [0], [0], [0], [0],
[0], [0], [0], [0], [0], [0]], form=2, user=a, id_sqlite=1, company=1},
5={date=2015-04-13, checkBox=[[0], [1], [0], [0], [0], [0], [0], [0],
[0], [0], [0], [0]], form=2, user=a, id_sqlite=6, company=3},
4={date=2015-04-13, checkBox=[[1], [0], [0], [0], [0], [0], [0], [0],
[0], [0], [0], [0]], form=1, user=a, id_sqlite=5, company=3}}"
04-14 14:07:07.938: D/Login Response(1038):
[]$id_sqlite,$user,$company,$form,$checkbox,$date)
04-14 14:07:07.948: W/System.err(1038): org.json.JSONException: Value
string(819) of type java.lang.String cannot be converted to JSONObject
04-14 14:07:07.968: W/System.err(1038): at
org.json.JSON.typeMismatch(JSON.java:111)
04-14 14:07:07.968: W/System.err(1038): at org.json.JSONObject.<init>
(JSONObject.java:158)
04-14 14:07:07.968: W/System.err(1038): at org.json.JSONObject.<init>
(JSONObject.java:171)
04-14 14:07:07.988: W/System.err(1038): at
com.example.php_mysql_sqlite.MainActivity$1.
onResponse(MainActivity.java:131)
04-14 14:07:07.988: W/System.err(1038): at
com.example.php_mysql_sqlite.MainActivity$1.
onResponse(MainActivity.java:1)
04-14 14:07:07.997: W/System.err(1038): at
com.android.volley.toolbox.StringRequest.
deliverResponse(StringRequest.java:60)
04-14 14:07:08.018: W/System.err(1038): at
com.android.volley.toolbox.StringRequest.
deliverResponse(StringRequest.java:30)
04-14 14:07:08.048: W/System.err(1038): at
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.
run(ExecutorDelivery.java:99)
04-14 14:07:08.048: W/System.err(1038): at
android.os.Handler.handleCallback(Handler.java:725)
04-14 14:07:08.048: W/System.err(1038): at
android.os.Handler.dispatchMessage(Handler.java:92)
04-14 14:07:08.057: W/System.err(1038): at
android.os.Looper.loop(Looper.java:137)
04-14 14:07:08.057: W/System.err(1038): at
android.app.ActivityThread.main(ActivityThread.java:5041)
04-14 14:07:08.057: W/System.err(1038): at
java.lang.reflect.Method.invokeNative(Native Method)
04-14 14:07:08.068: W/System.err(1038): at
java.lang.reflect.Method.invoke(Method.java:511)
04-14 14:07:08.068: W/System.err(1038): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.
run(ZygoteInit.java:793)
04-14 14:07:08.068: W/System.err(1038): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-14 14:07:08.068: W/System.err(1038): at
dalvik.system.NativeStart.main(Native Method)
Page php server
<?php
include_once 'include/DB_Functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = $_POST["json"];
var_dump($json);
$db->storeTemporal($json);
//Remove Slashes
if (get_magic_quotes_gpc()) {
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a = array();
$b = array();
//Loop through an Array and insert data read from JSON into MySQL DB
for ($i = 0; $i < count($data); $i++) {
//Store User into MySQL DB
$res = $db->guardar_chequeo($data[$i]->id_sqlite, $data[$i]->user, $data[$i]->company, $data[$i]->form,$data[$i]->checkbox, $data[$i]->date);
//Based on inserttion, create JSON response
if ($res) {
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a, $b);
} else {
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a, $b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>
Как вы можете видеть, если сервер получает и печатает, но выдает ошибку
PS: если вы дадите мне отрицательные моменты, пожалуйста, оставьте комментарий с тем, что я делаю неправильно, чтобы узнать, как это имеет случилось со мной иногда и не то, что отказ
Вы можете оставить ответ здесь, пожалуйста? –
@HeshanSandeepa Ответ, который мне дал, это то, что является журналом – user3613281
Я считаю, что ваш ответ недействителен, попробуйте здесь http://jsonlint.com/ –