我正在使用以下内容调用PHP脚本来记录用户:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new loginClass().execute();
}
});
}
class loginClass extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Logging you in...");
progressDialog.show();
progressDialog.setCancelable(false);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
loginClass.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
try {
HttpPost httpPost = new HttpPost("http://www.example.com/login.php");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 3000);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
EditText uname = (EditText)findViewById(R.id.usernameField);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.passwordField);
String password = pword.getText().toString();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
HttpClient httpClient = new DefaultHttpClient(httpParameters);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.e("log_tag", "Open Connection");
} catch(ConnectTimeoutException e){
Log.e("Timeout Exception: ", e.toString());
result = null;
} catch(SocketTimeoutException ste){
Log.e("Timeout Exception: ", ste.toString());
result = null;
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
result = null;
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
Log.e("log_tag", "Result: "+result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
result = null;
}
return null;
}
protected void onPostExecute(Void v) {
Log.e("log_tag", "Execute Started - Result: "+result);
if (result == null){
this.progressDialog.dismiss();
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Connection Error");
alertDialog.setMessage("There was a problem with your connection.");
alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.setButton2("Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new loginClass().execute();
}
});
alertDialog.show();
}else if("0".equals(result.toString())){
this.progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Invalid Username/Password. Please try again.", Toast.LENGTH_SHORT).show();
}else if("1".equals(result.toString())){
this.progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
}else{
this.progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Something Else Happened", Toast.LENGTH_SHORT).show();
}
}
}
}
在LogCat中,我显示Execute Started - Result: 0
,但它没有在if语句中捕获结果,因此最后一个else语句正在触发。 "0".equals(result.toString())
是处理结果的正确方法吗?
答案 0 :(得分:1)
使用String.contains()
方法。
试试result.toString().contains("0")
和result.toString().contains("1")
这应该工作。
我建议将响应检索为JSON对象而不是普通的HTTP响应。