我开发了一个 android 应用程序,当我在真实设备上运行该应用程序时遇到问题。在模拟器上一切正常。当活动开始时,我填写用户名和密码,然后按登录按钮。什么都没有发生,2-3 分钟后我有下面的错误。
这是 LogInActivity.java
package com.example.movenialapplication;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LogInActivity extends Activity{
IpString ipStr = new IpString();
static String ip = IpString.ipAdress;
private static String url_logIn = "http://"+ip+"/android_nydor/login3.php";
Button btnForLogIn;
JSONParser jsonParser = new JSONParser();
String username,password;
EditText usernameTxt;
EditText passwordTxt;
private static final String TAG_SUCCESS = "success";
private static final String TAG_USERS = "users";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in);
btnForLogIn = (Button) findViewById(R.id.LogInButton);
btnForLogIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
usernameTxt =(EditText) findViewById(R.id.username);
passwordTxt =(EditText) findViewById(R.id.password);
username = usernameTxt.getText().toString();
password = passwordTxt.getText().toString();
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
// getting product details by making HTTP request
// Note that product details url will use GET request
//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//StrictMode.setThreadPolicy(policy);
JSONObject json = jsonParser.makeHttpRequest(url_logIn, "GET", params);
//Log.i("jsonOb", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AllFromMovenialActivity.class);
// sending pid to next activity
//in.putExtra(TAG_ID, movenialID);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
else{
usernameTxt.setText("") ;
passwordTxt.setText("") ;
TextView t = (TextView)findViewById(R.id.WrongPassword);
t.setVisibility(View.VISIBLE);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
这是php代码:
<?php
//opening connection link
$link_fetch_data = mysql_connect('localhost:3306', 'root', 'pass');
$username=$_GET['username'];
$password=$_GET['password'];
if (!$link_fetch_data) {
die('Could not connect: ' . mysql_error());
}
//selecting db from server
mysql_select_db("sccm_nydor", $link_fetch_data);
$q=mysql_query("SELECT idusers FROM users WHERE username='".$_GET['username']."' AND password = SHA1(CONCAT('".$_GET['username']."','".$_GET['password']."')) AND active=1");
if (!empty($q)) {
// check for empty result
if (mysql_num_rows($q) > 0) {
$q = mysql_fetch_array($q);
$movenial_users = array();
$movenial_users["idusers"]=$q["idusers"];
// success
$response["success"] = 1;
// user node
$response["movenial_users"] = array();
array_push($response["movenial_users"], $movenial_users);
// echoing JSON response
echo json_encode($response);
}
else {
// no product found
$response["success"] = 0;
$response["message"] = "No contents found";
// echo no users JSON
echo json_encode($response);
}
}
else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
mysql_close($link_fetch_data);
?>
这是日志:
05-15 13:56:18.075: E/Buffer Error(7588): Error converting result java.lang.NullPointerException
05-15 13:56:18.075: E/JSON Parser(7588): Error parsing data org.json.JSONException: End of input at character 0 of
05-15 13:56:18.085: E/AndroidRuntime(7588): FATAL EXCEPTION: main
05-15 13:56:18.085: E/AndroidRuntime(7588): java.lang.NullPointerException
05-15 13:56:18.085: E/AndroidRuntime(7588): at com.example.movenialapplication.LogInActivity$1.onClick(LogInActivity.java:65)
05-15 13:56:18.085: E/AndroidRuntime(7588): at android.view.View.performClick(View.java:3538)
05-15 13:56:18.085: E/AndroidRuntime(7588): at android.view.View$PerformClick.run(View.java:14330)
05-15 13:56:18.085: E/AndroidRuntime(7588): at android.os.Handler.handleCallback(Handler.java:608)
05-15 13:56:18.085: E/AndroidRuntime(7588): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 13:56:18.085: E/AndroidRuntime(7588): at android.os.Looper.loop(Looper.java:156)
05-15 13:56:18.085: E/AndroidRuntime(7588): at android.app.ActivityThread.main(ActivityThread.java:4987)
05-15 13:56:18.085: E/AndroidRuntime(7588): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 13:56:18.085: E/AndroidRuntime(7588): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 13:56:18.085: E/AndroidRuntime(7588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-15 13:56:18.085: E/AndroidRuntime(7588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-15 13:56:18.085: E/AndroidRuntime(7588): at dalvik.system.NativeStart.main(Native Method)