这是我的代码。我和他们一起工作了 2 天。我认为我的问题出在 codeigniter 部分,因为我的 android 代码适用于普通 php。当我使用普通的 php 代码时,我收到了“登录成功”的消息。但是,当我使用 codeigniter 时,我收到了“有问题”的消息。我也收到此错误消息。
08-14 20:49:06.077: W/System.err(1660): org.json.JSONException: java.lang.String 类型的致命值无法转换为 JSONObject
08-14 20:49:06.077: W/System.err(1660): 在 com.example.test2.MainActivity$1.onClick(MainActivity.java:79)
这是我的代码。
安卓
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
// txtUsername & txtPassword
final EditText txtUser = (EditText) findViewById(R.id.txtUsername);
final EditText txtPass = (EditText) findViewById(R.id.txtPassword);
// btnLogin
final Button btnLogin = (Button) findViewById(R.id.btnLogin);
// Perform action on click
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = "http://sit-edu4.sit.kmutt.ac.th/csc498/53270323/Boss/sftrip/application/controllers/checkLogin.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("strUser", txtUser.getText()
.toString()));
params.add(new BasicNameValuePair("strPass", txtPass.getText()
.toString()));
String resultServer = getHttpPost(url, params);
/*** Default Value ***/
String success = "0";
String error = "0";
String error_msg = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
success = c.getString("success");
error = c.getString("error");
error_msg = c.getString("error_msg");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Login
if (error.equals("1")) {
// Dialog
ad.setTitle("Error! ");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setPositiveButton("Close", null);
ad.setMessage(error_msg);
ad.show();
txtUser.setText("");
txtPass.setText("");
} else if (success.equals("1")) {
Toast.makeText(MainActivity.this, "Login OK",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Something wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
public String getHttpPost(String url, List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
普通的php代码
<?
$objConnect = mysql_connect("mydb-pj.sit.kmutt.ac.th","s53270327csc498","Y44TECWK");
$objDB = mysql_select_db("s53270327csc498db");
//$_POST["strUser"] = "weerachai"; // for Sample
//$_POST["strUser"] = "weerachai@1"; // for Sample
$strUsername = $_POST["strUser"];
$strPassword = $_POST["strPass"];
$strSQL = "SELECT * FROM personal_information WHERE email = '$strUsername' AND password = '$strPassword'";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$intNumRows = mysql_num_rows($objQuery);
if($intNumRows==0)
{
$arr['success'] = "0";
$arr['error'] = "1";
$arr['error_msg'] = "Incorrect Username and Password";
}
else
{
$arr['success'] = "1";
$arr['error'] = "0";
$arr['error_msg'] = "";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
代码点火器
<?php
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
/*
load you helper library
*/
/*
load you model
*/
$this -> load -> model('login_model');
}
function test() {
//$tag = $this -> input -> post('tag');
//if ($tag == 'login') {
$email = $this -> input -> post("strUser");
$password = $this -> input -> post("strPass");
$response = array('success' => "0", 'error' => "0", 'error_msg' => "Unknown");
if ($email == null || $password == null) {
$response['error'] = "1";
$response['error_msg'] = "Both email and password must be filled";
//echo json_encode($response);
} else {
$query = $this -> login_model -> validate_email_password($email, $password);
if ($query) {
$arr['success'] = "1";
$arr['error'] = "0";
$arr['error_msg'] = "";
//echo json_encode($response);
} else {
$arr['success'] = "0";
$arr['error'] = "1";
$arr['error_msg'] = "Incorrect Username and Password";
}
}
echo json_encode($arr);
}
}
?>
请帮我。谢谢你。