我在我的應用程序中有以下類,我正在向遠程服務器發送用戶名和密碼,並在服務器端發送匹配值併發送響應。一切工作都很好。我想問如何在登錄成功消息後開始新的活動。消息消失時,我希望新的活動開始。 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));
}
}
「不成功」是什麼意思?應用程序是否會崩潰,什麼都不發生,或發生其他事情? 「不起作用」不是對問題的一個好的描述。 –
我不知道如何使用Asyncthask。 codeMagic提供的鏈接很好,但asynctask對我來說是新的。所以我不知道如何合併我的代碼與asynctask代碼。任何人都可以幫助我。 –
@ Tanis.7x成功登錄後,其他人表示Move_to_next()從未調用過。和其他人已經知道的logcat在主線程上顯示過多的工作。但我不知道如何在我的代碼中使用asynctask。 –