Java 登录后获取角色
当我发送登录后数据时,使用如下登录按钮:Java 登录后获取角色,java,android,json,http-post,Java,Android,Json,Http Post,当我发送登录后数据时,使用如下登录按钮: loginbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final String user = email.getText().toString().trim();
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
}
}
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxNCIsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMC4yMTk6OTBcL2F1dGhcL2xvZ2luIiwiaWF0IjoiMTQ0NTI0NzIyMSIsImV4cCI6IjE0NDY0NTY4MjEiLCJuYmYiOiIxNDQ1MjQ3MjIxIiwianRpIjoiNTY4MDEyZDUwNTg1NDFjM2UzNGVjMGViYzMzZDkzMGQifQ.zw5c5kLIlvPYIMhEzEnF_fCOu77XTq2prcDtSHJY7bk","role":"studio"}
如何获取角色(JSON响应反馈),以便在执行HTTP Post请求后使用if和elseif语句(在Utils.toast(context,responseEastText);)之后)
在我使用asynctask后更新,它不会在单击时产生错误。
更新完成后。谢谢你,什里亚什先生
问题结束。您不能在UI线程上运行长时间运行的任务。您需要使用线程或异步任务…重复的…请描述正确的异步任务,我不理解。这样地?私有类SendfeedbackJob扩展了AsyncTask保护的String doInBackground(String user,String pwd),我这样调用onclick:SendfeedbackJob job=new SendfeedbackJob();作业执行(用户,pwd);请参阅如何调用异步任务。因为有很多方法可以定义。我还更新了代码。像那样吗,什里亚什先生?但我仍然无法获得json对象角色。