org.json.JSONException:android的字符0处的输入结束 [英] org.json.JSONException: End of input at character 0 of, android
问题描述
我正在实现一个Android应用,并尝试进行注册和登录屏幕.我的应用已连接到服务器,这是我的代码中导致错误的部分:
I am implementing an android app and trying to make a register and login screens. my app is connected to a server, here is the part my code that makes error:
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
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");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else
{
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
Log.d("Debug", errorMsg);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.toString());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
这是我的错误: org.json.JSONException:输入的结束字符为0. 实际上,我在该网站上看到过类似的问题,但是我无法解决我的问题
and here is my error: org.json.JSONException: End of input at character 0 of. actually I have seen similar questions in this site but i was not be able to solve my problem
更新: 这是我的logcat内容,响应之前没有任何内容:
UPDATE: this is my logcat content, and there is nothing in front of response:
1576-1576/com.example.neshat.androidhive2 D/RegisterActivity﹕ Register Response:
12-21 09:43:08.820 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ org.json.JSONException: End of input at character 0 of
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at org.json.JSONTokener.nextValue(JSONTokener.java:97)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:156)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.example.neshat.androidhive2.RegisterActivity$3.onResponse(RegisterActivity.java:124)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.example.neshat.androidhive2.RegisterActivity$3.onResponse(RegisterActivity.java:116)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-21 09:43:08.824 1576-1576/com.example.neshat.androidhive2 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
推荐答案
正如您在logcat 1576-1576/com.example.neshat.androidhive2 D/RegisterActivity﹕ Register Response:
中看到的,这是Log.d(TAG, "Register Response: " + response.toString());
的结果,response
是一个空字符串,因此您的应用将获得org.json.JSONException: End of input at character 0 of
在JSONObject jObj = new JSONObject(response);
行.
As you have seen in your logcat 1576-1576/com.example.neshat.androidhive2 D/RegisterActivity﹕ Register Response:
which is the result of Log.d(TAG, "Register Response: " + response.toString());
, response
is an empty string, so your app will get org.json.JSONException: End of input at character 0 of
at the line JSONObject jObj = new JSONObject(response);
.
您应该先检查response
不为null,也不应该为空字符串.
You should check response
is not null and not an empty string first.
这篇关于org.json.JSONException:android的字符0处的输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!