我正在通过 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;
}
}