0

我正在通过 asynctask 使用 Web 服务进行登录。我正在传递用户名和密码的值,并返回返回 {"success":"true"} 或 {"success":"false"} 的 json。如果为 true,那么它应该触发意图并转到下一个活动,否则它应该显示不正确的用户名/密码,但我不知道为什么意图在 onPostExecute 中不起作用。请帮我。

这是我的代码

public class Dashboard extends Activity {

    TextView map;
    private String username;
    private String password;
    public String jArray;
    public Boolean login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        Button rtn=(Button)findViewById(R.id.buttonsubmit);
        EditText edit_text1=(EditText)findViewById(R.id.editText1);
        EditText edit_text2=(EditText)findViewById(R.id.editText2);
        String username=edit_text1.getEditableText().toString();
        Log.d("username",username);
    }

    public void onClick(View v)
    {
        EditText edit_text1=(EditText)findViewById(R.id.editText1);
        EditText edit_text2=(EditText)findViewById(R.id.editText2);
        String username=edit_text1.getEditableText().toString();
        String password=edit_text2.getEditableText().toString();

        if(username==null)
        {
            map.setText("Please enter something");
        }
        else if(username.isEmpty() || password.isEmpty())
        {
            Toast.makeText(this, "empty value", Toast.LENGTH_SHORT).show();
        }
        else
        {
        Log.d("user",username);
        Log.d("password",password);
        new MyAsyncTaskMapNoGet().execute();

        }
    }

    public boolean webservreqMAPNOGET(){
        try {
            //HttpParams httpParams = new BasicHttpParams();
            //HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
            //HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);            
            HttpParams p = new BasicHttpParams();
            p.setParameter("user", "1");
            // Instantiate an HttpClient
            HttpClient httpclient = new DefaultHttpClient(p);
            EditText edit_text1=(EditText)findViewById(R.id.editText1);
            EditText edit_text2=(EditText)findViewById(R.id.editText2);
            String username=edit_text1.getEditableText().toString();
            String password=edit_text2.getEditableText().toString();
            Log.d("username1",username);
            String url = url;
            Log.d("url",url );

            HttpPost httppost = new HttpPost(url);

            // Instantiate a GET HTTP method
            try {
                Log.i(getClass().getSimpleName(), "send  task - start");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("user", "1"));
                httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httppost, responseHandler);
                Log.d("respnse",responseBody);

                // Parse
                JSONObject json1 = new JSONObject(responseBody);
                String jArray = json1.getString("success");
                Log.d("success", jArray);
                if(jArray=="true")
                {
                Boolean login=true;
                }
                else
                {
                    Boolean login=false;
                }
                return true;
            } 
            catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } 
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        } 
        catch (Throwable t) {
            //Toast.makeText(this, "Request failed: " + t.toString(),Toast.LENGTH_LONG).show();
            return false;
        }        
    }

     public class MyAsyncTaskMapNoGet extends AsyncTask<Void, Void, Boolean>
        {        
            ProgressDialog mProgressDialog3;
            private String json1;
            @Override
            public void onPostExecute(Boolean result) {
                mProgressDialog3.dismiss();
                mProgressDialog3.dismiss();
                mProgressDialog3.dismiss();
            }

            @Override
            public void onPreExecute() {
                mProgressDialog3 = ProgressDialog.show(Dashboard.this, "Loading...", "Data is Loading...");
            }

            @Override
            public Boolean doInBackground(Void... params) {
                if(webservreqMAPNOGET()){
                    Log.d("yay","SUCCESS");

                    return true;
                }
                else{
                    Log.d("err","ERROR");
                    return false;
                }
            }
                public void onPostExecute() { 
                    // dismiss the dialog once done 

                    if(login)
{       
                    startActivity(new Intent("com.example.esstel___ippbx.secondactivity"));
}                       
                        else
                        {
                            Toast.makeText(Dashboard.this, "incorrect username/password", 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.dashboard, menu);
        return true;
    }

}
4

2 回答 2

0

改变你onPostExecute

   @Override
   public void onPostExecute(Boolean result) {
   super.onPostExecute(result);
   mProgressDialog3.dismiss(); 
   if(result) // true
    { 
            // start activity    
    }
   else
   {
            // display toast 
   }
   } 

为什么你有 3 个语句 mProgressDialog3.dismiss()删除其他两个。

也删除这个你已经有一个覆盖onPostExecute

   public void onPostExecute() { 

您还可以从 doInbackground 中的 editext 获取文本。将edittext 初始化为类成员。中初始化onCreate。获取按钮单击时的文本。将值直接传递给 doInbackground 或构造函数 asynctask 并使用相同的值。

  public class Dashboard extends Activity implements OnClickListenr{

在 onCreate

  rtn.setOnClickListener(this);

也不需要每次都初始化视图。onCreate一次初始化

于 2013-09-07T09:16:32.037 回答
0

您的异步任务中有两个 postExecute 方法。在您的自定义 postexecute 方法(不是被覆盖的方法)中提到了传输到新活动的逻辑。由于您有两个 postexecutes,因此您的 async 任务仅识别第一个被覆盖的一个。您需要从覆盖的方法中调用您的自定义 postexecute 方法,例如::

 @Override
        public void onPostExecute(Boolean result) {
            mProgressDialog3.dismiss();
            mProgressDialog3.dismiss();
            mProgressDialog3.dismiss();
            onPostExecute();
        }

您的 TextView 地图也未初始化。您正在对未初始化的 textView 执行 setText() ,因此它会给出 NPE。

于 2013-09-07T09:28:41.837 回答