У любого есть идея, как разобрать этот ответ JSON. Также скажите мне, почему у меня есть [enter you code here]
в моем ответе JSON. Я понятия не имею, откуда эта строка.
мой LogCat это:
03-30 09:49:16.757: W/dalvikvm(13459): VFY: unable to resolve static method 604: Landroid/net/TrafficStats;.setThreadStatsTag (I)V
03-30 09:49:16.757: D/dalvikvm(13459): VFY: replacing opcode 0x71 at 0x000a
03-30 09:49:16.976: D/dalvikvm(13459): GC_CONCURRENT freed 206K, 47% free 2997K/5639K, external 507K/517K, paused 3ms+4ms
03-30 09:49:18.976: I/global(13459): Loaded time zone names for en_US in 1110ms.
03-30 09:49:18.992: D/RegisterActivity(13459): Register Response: [Enter code here]{"tag":"register","error":false,"uid":"5518cec863a130.84373662","user":{"name":"kku","email":"[email protected]","created_at":"2015-03-30 00:19:20","updated_at":null}}
03-30 09:49:18.992: D/RegisterActivity(13459): <!-- Hosting24 Analytics Code -->
03-30 09:49:18.992: D/RegisterActivity(13459): <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
03-30 09:49:18.992: D/RegisterActivity(13459): <!-- End Of Analytics Code -->
03-30 09:49:18.992: D/RegisterActivity(13459): user added
03-30 09:49:19.000: W/System.err(13459): org.json.JSONException: Unterminated array at character 8 of [Enter code here]{"tag":"register","error":false,"uid":"5518cec863a130.84373662","user":{"name":"kku","email":"[email protected]","created_at":"2015-03-30 00:19:20","updated_at":null}}
03-30 09:49:19.000: W/System.err(13459): <!-- Hosting24 Analytics Code -->
03-30 09:49:19.000: W/System.err(13459): <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
03-30 09:49:19.000: W/System.err(13459): <!-- End Of Analytics Code -->
03-30 09:49:19.000: W/System.err(13459): at org.json.JSONTokener.syntaxError(JSONTokener.java:446)
03-30 09:49:19.007: W/System.err(13459): at org.json.JSONTokener.readArray(JSONTokener.java:436)
03-30 09:49:19.007: W/System.err(13459): at org.json.JSONTokener.nextValue(JSONTokener.java:99)
03-30 09:49:19.007: W/System.err(13459): at org.json.JSONObject.<init>(JSONObject.java:154)
03-30 09:49:19.007: W/System.err(13459): at org.json.JSONObject.<init>(JSONObject.java:171)
03-30 09:49:19.007: W/System.err(13459): at com.example.classroom.RegisterActivity$3.onResponse(RegisterActivity.java:125)
03-30 09:49:19.007: W/System.err(13459): at com.example.classroom.RegisterActivity$3.onResponse(RegisterActivity.java:1)
03-30 09:49:19.007: W/System.err(13459): at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
03-30 09:49:19.007: W/System.err(13459): at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-30 09:49:19.007: W/System.err(13459): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-30 09:49:19.007: W/System.err(13459): at android.os.Handler.handleCallback(Handler.java:587)
03-30 09:49:19.007: W/System.err(13459): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 09:49:19.007: W/System.err(13459): at android.os.Looper.loop(Looper.java:130)
03-30 09:49:19.007: W/System.err(13459): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-30 09:49:19.015: W/System.err(13459): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 09:49:19.015: W/System.err(13459): at java.lang.reflect.Method.invoke(Method.java:507)
03-30 09:49:19.015: W/System.err(13459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-30 09:49:19.015: W/System.err(13459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-30 09:49:19.015: W/System.err(13459): at dalvik.system.NativeStart.main(Native Method)
03-30 09:50:34.953: W/KeyCharacterMap(13459): Can't open keycharmap file
03-30 09:50:34.953: W/KeyCharacterMap(13459): Error loading keycharmap file
03-30 09:50:34.953: W/KeyCharacterMap(13459): Using default keymap
03-30 09:50:35.234: W/IInputConnectionWrapper(13459): showStatusIcon on inactive InputConnection
03-30 09:52:17.656: D/dalvikvm(13459): GC_EXPLICIT freed 216K, 47% free 3027K/5639K, external 507K/517K, paused 46ms
Register Response:
это текст из моего кода Java для logcat. Для вашей информации я использую залп.
мой исходный код Java для анализа JSON:
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response);
hideDialog();
try {
Log.d(TAG, "user added");JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
Вот мой PHP-код:
index.php
<?php
/**
* File to handle all API requests
* Accepts GET and POST
*
* Each request will be identified by TAG
* Response will be JSON data
/**
* check for POST request
*/
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "error" => FALSE);
// check for tag type
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user not found
// echo json with error = 1
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
} else if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = TRUE;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
// store user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Error occured in Registartion";
echo json_encode($response);
}
}
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
?>
Этот код, который вы можете использовать сейчас, который пропустит [Введите ваш код] сейчас
response = response.split("]")[1];
JSONObject jo = new JSONObject(response);
String tag = jo.getString("tag");
boolean error = jo.getBoolean("error");
String uid = jo.getString("uid");
JSONObject jo1 = jo.getJSONObject("user");
String name = jo1.getString("name");
String email = jo1.getString("email");
String createdAt = jo1.getString("created_at");
String updatedAt = jo1.getString("updated_at");
System.out.println("Tag: " + tag + " error: " + error + " name: "+ name + " email: " + email);
Я думаю, прежде чем кодировать данные в JSON из php, вы должны распечатать данные и проверить, что вы отправляете. Возможно, вы отправляете неверные данные, потому что volly не мешает.
Других решений пока нет …