0

我想做一个表单登录,如果登录不成功,它会给出字符串响应“2”,我已经尝试过,但是我出错了。这是我的异步任务:

public class LoginAsyncTask extends AsyncTask<Void, Void, String>
    {
        String strUser = txtUsername.getText().toString();
        String strPass = txtPassword.getText().toString();
        String strUrlMain="http://www.abc.co.id/a/bb/login.htm?u="+strUser+"&p="+strPass;
        String str2;

        public LoginAsyncTask(){
            this.strUrlMain=strUrlMain; 
            System.out.println(strUrlMain);
        }


        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        Dialog = ProgressDialog.show(LoginProposal.this, "", "Mohon Tunggu, Sedang Melakukan Verifikasi");
        }

        @Override
        protected String doInBackground(Void... params) {
            String result="";
            try {
                result=Connection.get(strUrlMain);
            }
            catch (Exception e){
                result=" ";
                }
            return result;
        }

        @Override
        protected void onPostExecute(String result) 
        {
            if (!result.equals("")) {
                if(str2.equals("2"))
                {
                     Dialog.dismiss();
                     AlertDialog.Builder builder = new AlertDialog.Builder(LoginProposal.this);
                        builder.setTitle("Login Gagal");
                        builder.setMessage("User ID atau password salah.");
                     builder.setPositiveButton("Coba Kembali",
                             new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             txtPassword.setText("");
                             txtPassword.findFocus();
                             btnLogin.setClickable(true);
                         }
                     });
                        builder.setNegativeButton("Keluar",new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialog, int which) {

                                   finish();
                               }
                           });
                        builder.create().show();
                }

                else{
                    Dialog.dismiss();
                    Intent slide2 = new Intent(LoginProposal.this, DataProposal.class);

                    startActivity(slide2);

        }

这是我的连接方法:

public class Connection {

    public static String get(String url) throws IOException
    {
        String apiResponse = "";
        try {
            String urlFix = url;
            HttpClient client = new DefaultHttpClient();
            HttpGet post = new HttpGet(urlFix);
            HttpResponse response = client.execute(post);

            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                throw new Exception("Got HTTP " + statusCode + " ("
                        + response.getStatusLine().getReasonPhrase() + ')');
            }

            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            if(entity != null)
            {

                //"iso-8859-1"
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);

                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                        sb.append(line + "\n");
                }
                is.close();

                apiResponse = sb.toString();
            }
            else
            {
                apiResponse = "";
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return apiResponse;
    }
}

这就是我的 logcat 所说的:

10-03 10:55:54.436: E/AndroidRuntime(7561): FATAL EXCEPTION: main
10-03 10:55:54.436: E/AndroidRuntime(7561): java.lang.NullPointerException
10-03 10:55:54.436: E/AndroidRuntime(7561):     at id.co.asjmsig.eproposal.LoginProposal$LoginAsyncTask.onPostExecute(LoginProposal.java:80)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at id.co.asjmsig.eproposal.LoginProposal$LoginAsyncTask.onPostExecute(LoginProposal.java:1)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at android.os.Looper.loop(Looper.java:137)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at android.app.ActivityThread.main(ActivityThread.java:4895)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at java.lang.reflect.Method.invoke(Method.java:511)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
10-03 10:55:54.436: E/AndroidRuntime(7561):     at dalvik.system.NativeStart.main(Native Method)

LoginProposal.java:80 是if(str2.equals("2")),我不知道我的错在哪里,希望有人能帮我解决,谢谢

4

2 回答 2

1

您永远不会为str2变量赋值。这就是错误。

于 2013-10-03T04:02:44.393 回答
0
//try this way
String str2="";
于 2013-10-03T04:08:04.133 回答