我正在尝试使用loopj asynctask在android上登录以链接到数据库,但我有问题。好像JSONObject不起作用。帮我检查一下我是否犯了错误。这是我的代码
package com.truparsecreative.uimobile;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONException;
import org.json.JSONObject;
public class login extends ActionBarActivity {
public EditText username, password;
public Button login_button;
ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
//login_button = (Button) findViewById(R.id.login);
progress = new ProgressDialog(this);
progress.setMessage("Connecting...");
progress.setCancelable(false);
}
public void loginUser(View view) {
String user = username.getText().toString();
String pass = password.getText().toString();
RequestParams params = new RequestParams();
if (user != "" && pass != "") {
params.put("username", user);
params.put("password", pass);
invokeWS(params);
} else {
Toast.makeText(getApplicationContext(), "Matric and Password cannot be empty", Toast.LENGTH_LONG).show();
}
}
public void invokeWS(RequestParams params) {
progress.show();
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://10.0.2.2/uimobile/app/android/login.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String content) {
super.onSuccess(content);
progress.hide();
try {
JSONObject jsonObject = new JSONObject(content);
String mat = jsonObject.getString("matric");
if (mat !=null)
{
Intent loginIntent = new Intent(getApplicationContext(), profile.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Throwable error,String content) {
//super.onFailure(error, content);
progress.hide();
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
并且login.php代码是
<?php require_once('connect.php');
$username = $_POST['username'];
$password = $_POST['password'];
$c_login = mysql_query("SELECT * FROM users WHERE matric = '$username' AND password = '$password'");
if (mysql_num_rows($c_login)>0)
{
echo "Correct Details";
//$output = array();
$content = mysql_fetch_array($c_login);
$output = array(
'matric'=> $content['matric'],
'password'=> $content['password'],
'email'=> $content['email']
);
print json_encode($output);
}
else
{
echo "Username and password didn't match".mysql_error();
}
?>
答案 0 :(得分:0)
在PHP代码中,您在发送JSON对象之前执行此操作:
echo "Correct Details";
由于该行,您的PHP代码不会向Android应用程序发送有效的JSON对象。 PHP脚本的结果将具有以下格式:
Correct Details
{
matric: "some value",
password: "some value",
email: "some value"
}
由于Correct Details
echo "Correct Details";
,这不是有效的JSON对象