这是我的Login.java
public class Login extends ActionBarActivity implements OnClickListener {
private Button login,register;
private EditText email,password;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.14:1234/PMSS/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.registerlauncher);
email = (EditText) findViewById(R.id.userid);
password = (EditText) findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Username = email.getText().toString();
String Password = password.getText().toString();
new AttemptLogin(Username,Password).execute();
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
/*case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(Login.this, Register.class);
startActivity(i);
break;
*/
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, Integer> {
boolean failure = false;
String res;
String Username;
String Password;
int success;
public AttemptLogin(String Username, String Password) {
this.Username = Username;
this.Password = Password;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected Integer doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
params.add(new BasicNameValuePair("password", Password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
System.out.print("Here");
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
res = json.getString(TAG_MESSAGE);
return success;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Integer success) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (success != null && success == 1) {
Log.d("Login Successful!", "res: " + res);
Intent i = new Intent(Login.this, MainMenu.class);
startActivity(i);
Toast.makeText(Login.this, res == null? "Please enter user id and password" : res, Toast.LENGTH_LONG).show();
email.setText(null);
password.setText(null);
}else{
Log.d("Login Failure!", "res: " + res);
Toast.makeText(Login.this, res == null? "Please enter user id and password" : res, Toast.LENGTH_LONG).show();
}
}
}
}
这是我的logcat
12-08 17:12:28.559: D/request!(8709): starting
12-08 17:12:29.109: E/JSON Parser(8709): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
12-08 17:12:29.109: D/Login attempt(8709): {"message":"Please fill in the login details!"}
12-08 17:12:29.119: W/System.err(8709): org.json.JSONException: No value for success
12-08 17:12:29.119: W/System.err(8709): at org.json.JSONObject.get(JSONObject.java:354)
12-08 17:12:29.119: W/System.err(8709): at org.json.JSONObject.getInt(JSONObject.java:443)
12-08 17:12:29.119: W/System.err(8709): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:143)
12-08 17:12:29.119: W/System.err(8709): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:1)
12-08 17:12:29.119: W/System.err(8709): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-08 17:12:29.119: W/System.err(8709): at java.lang.Thread.run(Thread.java:1019)
12-08 17:12:29.129: D/Login Failure!(8709): res: null
第143行:success = json.getInt(TAG_SUCCESS);
为什么我输入了错误的用户ID或密码,并且没有说您输入了错误的ID或密码。 或者是我没有编写用于错误处理的java代码?我非常感谢你所有的帮助......
答案 0 :(得分:2)
当您输入错误的凭据时,您会收到此回复 -
{“message”:“请填写登录详细信息!”}
它们在响应中没有“成功”键,但在代码中你正在解析它的值,这就是它给出JSONExpection的原因。