1

我的应用程序中有以下课程,我将用户名和密码发送到远程服务器,并在服务器端匹配值并发送响应。一切正常。我想问一下登录成功消息后如何开始新活动。我希望在消息消失时开始新的活动。QnActivity 是我要开始的活动,而 LoActivity 是我当前的活动。我已经尝试了很多但没有成功。我也加了

startActivity(new Intent(LoActivity.this, QnActivity.class));

public void Move_to_next()方法中,但它不起作用。

Java代码-

 public class LoActivity extends Activity {

        Intent i;
        Button signin;
        TextView error;
        CheckBox check;
        String name="",pass="";
        byte[] data;
        HttpPost httppost;
        StringBuffer buffer;
        HttpResponse response;
        HttpClient httpclient;
        InputStream inputStream;
        SharedPreferences app_preferences ;
        List<NameValuePair> nameValuePairs;
        EditText editTextId, editTextP;

        @Override
        public void onCreate (Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            signin = (Button) findViewById (R.id.signin);
            editTextId = (EditText) findViewById (R.id.editTextId);
            editTextP = (EditText) findViewById (R.id.editTextP);
            app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
            check = (CheckBox) findViewById(R.id.check);
            String Str_user = app_preferences.getString("username","0" );
            String Str_pass = app_preferences.getString("password", "0");
            String Str_check = app_preferences.getString("checked", "no");
            if(Str_check.equals("yes"))
            {
                editTextId.setText(Str_user);
                editTextP.setText(Str_pass);
                check.setChecked(true);
            }

            signin.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    name = editTextId.getText().toString();
                    pass = editTextP.getText().toString();
                    String Str_check2 = app_preferences.getString("checked", "no");
                    if(Str_check2.equals("yes"))
                    {
                        SharedPreferences.Editor editor = app_preferences.edit();
                        editor.putString("username", name);
                        editor.putString("password", pass);
                        editor.commit();
                    }
                    if(name.equals("") || pass.equals(""))
                    {
                         Toast.makeText(Lo.this, "Blank Field..Please Enter", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {

                    try {
                        httpclient = new DefaultHttpClient();
                        httppost = new HttpPost("http://abc.com/register.php");
                        // Add your data
                        nameValuePairs = new ArrayList<NameValuePair>(2);
                        nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
                        nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        // Execute HTTP Post Request
                        response = httpclient.execute(httppost);
                        inputStream = response.getEntity().getContent();

                        data = new byte[256];

                        buffer = new StringBuffer();
                        int len = 0;
                        while (-1 != (len = inputStream.read(data)) )
                        {
                            buffer.append(new String(data, 0, len));
                        }

                        inputStream.close();
                    }

                    catch (Exception e)
                    {
                        Toast.makeText(LoActivity.this, "error"+e.toString(), Toast.LENGTH_SHORT).show();
                    }
                    if(buffer.charAt(0)=='Y')
                    {
                        Toast.makeText(LoActivity.this, "login successfull", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(LoActivity.this, "Invalid Username or password", Toast.LENGTH_SHORT).show();
                    }
                    }
                }
            });

        check.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                // Perform action on clicks, depending on whether it's now checked
                SharedPreferences.Editor editor = app_preferences.edit();
                if (((CheckBox) v).isChecked())
                {
                     editor.putString("checked", "yes");
                     editor.commit();
                }
                else
                {
                     editor.putString("checked", "no");
                     editor.commit();
                }
        }
        });
        }
         public void Move_to_next()
         {
             startActivity(new Intent(LoActivity.this, QnActivity.class));

         }
    }
4

3 回答 3

2

您正在 ui 线程上运行网络相关操作。使用线程或异步任务

  response = httpclient.execute(httppost);

NetworkOnMainThreadException如果您在 ui 线程上运行与网络相关的操作,您将获得post honeycomb

并确保你调用Move_to_next()你的代码。

AsyncTask在 ui 线程上调用

   new TheTask().execute();

异步任务

   class TheTask extends AsyncTask<Void,Void,Void>
   {
       @Override
       protected void onPreExecute()
       {
               super.onPreExecute();
               // dispaly progress dialog 
       } 
       @Override
       protected void doInbackground(Void... params)
       {
           // do network related operation here
           // do not update ui here
          return null; // return result here
       } 
       @Override
       protected void onPostExecute(Void result) // result of background computation received
       {
           super.onPostExecute(result);
            // dimiss dialog
           // update ui here     
       } 
   }  
于 2013-09-25T15:37:44.140 回答
2

很难说为什么它不工作,因为你没有告诉我们它是如何不工作的。但是,您应该将您的网络调用转移到另一个Thread. 把它放在一个AsyncTask. 在doInBackground().

然后在网络内容完成后,如果登录成功,您可以将结果发送到那里onPostExecute()并从那里调用。startActivity()

AsyncTask 文档

异步任务示例

于 2013-09-25T15:39:10.720 回答
2

我的猜测是,您永远不会调用该方法Move_to_next()

我建议您在从服务器获得良好响应后调用它,而且,您应该接受@Ragunandan 的建议并在单独的线程上运行请求。

于 2013-09-25T15:39:50.293 回答