当我发送登录后的数据时,请使用以下登录按钮:
loginbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String user = email.getText().toString().trim();
final String pwd = password.getText().toString().trim();
if (email.getText().toString().equals("")) {
Utils.toast(context, "Username empty...");
} else if (password.getText().toString().equals("")) {
Utils.toast(context, "Password empty...");
} else if (!isValidEmail(user)) {
email.setError("Invalid Email");
} else if (user.length() < 2) {
Utils.toast(context, "Username to short...");
} else if (pwd.length() < 2) {
Utils.toast(context, "Password to short...");
} else if (!isValidPassword(pwd)) {
password.setError("Invalid Password");
} else {
progress.setVisibility(View.VISIBLE);
SendfeedbackJob job = new SendfeedbackJob();
job.execute(user, pwd);
}
}
});
private class SendfeedbackJob extends AsyncTask<String, Void, String> {
private static final String LOG_TAG = "UserLoginTask";
@Override
protected String doInBackground(String... params) {
String user = params[0];
String pwd = params[1];
// do above Server call here
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("email", user ));
postParameters.add(new BasicNameValuePair("password", pwd ));
String responseString = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.219:90/auth/login");
// no idea what this does :)
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
// This is the line that send the request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseAsText = EntityUtils.toString(response.getEntity());
Utils.log("daftar isi: " + responseAsText);
JSONObject loginjson = new JSONObject(responseAsText);
Utils.log("json object: " + loginjson);
String roleString = loginjson.getString("role");
Utils.log("role: " + roleString);
if(roleString.equals("member")){
Intent intent = new Intent(context, home.class);
startActivity(intent);
}else if(roleString.equals("studio")){
Intent intent = new Intent(context, VendorDashboard.class);
startActivity(intent);
}
}
catch (Exception e)
{
Log.e(LOG_TAG, String.format("Error during login: %s", e.getMessage()));
}
return "processing";
}
@Override
protected void onPostExecute(String message) {
//process message
}
}
实际上它会得到像这样的反馈JSON响应:
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxNCIsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMC4yMTk6OTBcL2F1dGhcL2xvZ2luIiwiaWF0IjoiMTQ0NTI0NzIyMSIsImV4cCI6IjE0NDY0NTY4MjEiLCJuYmYiOiIxNDQ1MjQ3MjIxIiwianRpIjoiNTY4MDEyZDUwNTg1NDFjM2UzNGVjMGViYzMzZDkzMGQifQ.zw5c5kLIlvPYIMhEzEnF_fCOu77XTq2prcDtSHJY7bk","role":"studio"}
如何获得角色(JSON响应反馈)所以我可以在执行HTTP Post请求之后使用if和else if语句(在Utils.toast之后(context,responseAsText);)?
使用asynctask后更新,它没有产生错误onclick。 更新完成。谢谢mr.shreyash。
问题已经结束。