我正在尝试使用androidstudio和slimframework构建一个Android登录/注册应用程序,一切都适用于登录但是当我尝试注册时,它显示org.json.JSONException:字符0的输入结束。
这是我的主要活动代码:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.kosalgeek.asynctask.AsyncResponse;
import com.kosalgeek.asynctask.PostResponseAsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener {
TextView txtRegister;
EditText etUsername, etPassword;
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
txtRegister = (TextView) findViewById(R.id.edtRegister);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
txtRegister.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),RegistrationActivity.class);
startActivity(i);
}
});
}
@Override
public void processFinish(String result) {
JSONObject parentObject = null;
try {
parentObject = new JSONObject(result);
JSONObject response = parentObject.getJSONObject("response");
String error = response.getString("error");
if(error.equals("true"))
{
String message = response.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
else if(error.equals("false"))
{
Toast.makeText(this, "Connexion réussie", Toast.LENGTH_LONG).show();
String apiKey = response.getString("apiKey");
Intent i = new Intent(getApplicationContext(),DashBoardActivity.class);
i.putExtra("api_key", apiKey);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
String name = etUsername.getText().toString();
String password = etPassword.getText().toString();
if(name.isEmpty() || password.isEmpty()){
Toast.makeText(this, "Veuillez rentrer votre adresse mail et mot de passe", Toast.LENGTH_LONG).show();
}else{
HashMap loginData = new HashMap();
loginData.put("mobile","android");
loginData.put("email",name);
loginData.put("password",password);
PostResponseAsyncTask loginTask = new PostResponseAsyncTask(this, loginData);
loginTask.execute("http://10.0.3.2/androidapi/v1/login");
}
}
}
这是我的注册活动代码:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.kosalgeek.asynctask.AsyncResponse;
import com.kosalgeek.asynctask.EachExceptionsHandler;
import com.kosalgeek.asynctask.PostResponseAsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.util.HashMap;
import okhttp3.Response;
public class RegistrationActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener {
EditText edtNomComplet, edtPasswordR, edtAdresseMail;
Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
edtNomComplet = (EditText)findViewById(R.id.edtNomComplet);
edtPasswordR = (EditText)findViewById(R.id.edtRPassword);
edtAdresseMail = (EditText)findViewById(R.id.edtAdresseMail);
btnRegister = (Button)findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(this);
}
@Override
public void processFinish(String result) {
JSONObject parentObject = null;
try {
parentObject = new JSONObject(result);
Boolean success = parentObject.getBoolean("success");
JSONObject response = parentObject.getJSONObject("response");
Log.d("appandroid",success.toString());
String error = response.getString("error");
if(error.equals("true"))
{
String message = response.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
else if(error.equals("false"))
{
Toast.makeText(this, "Inscription réussie", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
@Override
public void onClick(View v) {
String name = edtNomComplet.getText().toString();
String email = edtAdresseMail.getText().toString();
String password = edtPasswordR.getText().toString();
if(name.isEmpty() || password.isEmpty() || email.isEmpty()){
Toast.makeText(this, "Veuillez rentrer les informations complétes", Toast.LENGTH_LONG).show();
}else{
HashMap registrationData = new HashMap();
registrationData.put("password", password);
registrationData.put("email", email);
registrationData.put("name", name);
registrationData.put("mobile","android");
Log.d("Appandroid", registrationData.toString());
PostResponseAsyncTask postTask = new PostResponseAsyncTask(this, registrationData);
postTask.execute("http://10.0.3.2/androidapi/v1/register");
}
}
}
我用RESTCLIENT测试了API,一切正常,我得到了JSON格式的结果。但是,当我尝试使用它显示的应用程序进行注册时:
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: org.json.JSONException: End of input at character 0 of
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at org.json.JSONObject.<init>(JSONObject.java:155)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at com.example.theapplication.appandroid.RegistrationActivity.processFinish(RegistrationActivity.java:52)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at com.kosalgeek.asynctask.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:248)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at com.kosalgeek.asynctask.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:31)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:632)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at android.os.Looper.loop(Looper.java:136)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-22 13:09:01.204 28738-28738/com.example.theapplication.appandroid W/System.err: at dalvik.system.NativeStart.main(Native Method)