我找不到错误

时间:2013-11-12 05:01:21

标签: android

public class Login extends Activity implements OnClickListener {

    private EditText user, pass;
    private Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // php login script location:

    // localhost :
    // testing on your device
    // put your local ip instead, on windows, run CMD > ipconfig
    // or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String LOGIN_URL =
    // "http://xxx.xxx.x.x:1234/webservice/login.php";
    private static final String LOGIN_URL = "http://192.168.2.4/mybringbacktutorial/login.php";

    // testing on Emulator:
    //private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";

    // testing from a real server:
    // private static final String LOGIN_URL =
    // "http://www.mybringback.com/webservice/login.php";

    // JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // setup input fields
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        // setup buttons
        mSubmit = (Button) findViewById(R.id.login);
        mRegister = (Button) findViewById(R.id.register);

        // register listeners
        mSubmit.setOnClickListener(this);
        mRegister.setOnClickListener(this);

    }

    @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(this, Register.class);
            startActivity(i);
            break;

        default:
            break;
        }
    }

    class AttemptLogin extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            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);

                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    // save user data
                    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Login.this);
                    Editor edit = sp.edit();
                    edit.putString("username", username);
                    edit.commit();

                    Intent i = new Intent(Login.this, ReadComments.class);
                    //finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }

}

---- --- logcat中

11-12 13:05:16.982: E/AndroidRuntime(28094): FATAL EXCEPTION: AsyncTask #1
11-12 13:05:16.982: E/AndroidRuntime(28094): java.lang.RuntimeException: An error occured while executing doInBackground()
11-12 13:05:16.982: E/AndroidRuntime(28094):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.lang.Thread.run(Thread.java:856)
11-12 13:05:16.982: E/AndroidRuntime(28094): Caused by: java.lang.NullPointerException
11-12 13:05:16.982: E/AndroidRuntime(28094):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:125)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:1)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-12 13:05:16.982: E/AndroidRuntime(28094):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-12 13:05:16.982: E/AndroidRuntime(28094):    ... 5 more

2 个答案:

答案 0 :(得分:1)

我相信您正在移动应用程序中测试它,您是否考虑关闭防火墙并关闭PC中的任何防病毒程序?

答案 1 :(得分:0)

试试如下:

    @Override
    protected String doInBackground(String... args) {
     if (success == 1) {
                Log.d("Login Successful!", json.toString());
                // save user data
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Login.this);
                Editor edit = sp.edit();
                edit.putString("username", username);
                edit.commit();
            } else {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));

            }

           return json.getString(TAG_MESSAGE);
     }
    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
              Intent i = new Intent(Login.this, ReadComments.class);
              startActivity(i);
        }

    }