我正在实现一个Android应用程序,并尝试进行注册和登录屏幕。我的应用程序连接到服务器,这是我的代码产生错误的部分:
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的输入结束。 实际上我在这个网站上看到过类似的问题,但我无法解决我的问题
更新: 这是我的logcat内容,并且没有任何回应:
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)
答案 0 :(得分:2)
正如您在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);
{1}}。
您应该先检查response
是否为空,而不是空字符串。
答案 1 :(得分:0)
如果您使用 php 获取响应,您应该检查您的 php 文件。我和你有同样的问题,我忘了在我的 php 文件上写响应。以下是我忘记的部分代码
$response = new emp();
$response->success = 1;
$response->message = "Register success, please login.";
die(json_encode($response));