我试过了 为登录做一个活动 我曾经使用过AsyncTask 我试试 随着 错误的凭据它没有显示任何弹出错误的密码。我已经尝试了所有验证的可能性,但当它进入AsyncTask它匹配参数但从不发布任何东西。所以我应该在哪里添加Alertdialog,使其匹配结果带参数和返回。我还需要获取json字符串。它是在onpostExecute方法或doinbackground中检查的。我是android pls help的新手。
public class FetchTask extends AsyncTask<String, Void, String> {
protected void onPostExecute(String result)
{
Emp emp=new Emp();
if(result==null && !(result.equals("0")) )
{
alert.showAlertDialog(MainActivity.this, "Login failed..", "Wrong credential please enter again", false);
// Toast.makeText(getApplicationContext(), "Wrong credentials Please enter again", Toast.LENGTH_SHORT).show();
}
List<Emp> data=new ArrayList<>();
try {
// JSONArray jArray = new JSONArray(result);
// for(int i=0;i<jArray.length();i++) {
JSONObject json_data = new JSONObject(result);
emp.validstatus=json_data.getInt("validstatus");
emp.roleid=json_data.getInt("roleid");
emp.empid=json_data.getString("empid");
data.add(emp);
if (emp.validstatus==1 && emp.roleid==14)
{
Intent intent=new Intent(MainActivity.this,Second.class);
intent.putExtra("empid",emp.empid);
startActivity(intent);
}
else
{
alert.showAlertDialog(MainActivity.this, "Login failed..", "Wrong credential please enter again", false);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
Log.e("reached","reaching");
Log.v(TAG,"Before intent object");
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// String[] loginVal = params[0].split(";");
//String build="http://192.168.0.102/login.php?user_name="+params[0].split(";")[0]+"&password="+params[0].split(";")[1];
//String build="http://192.168.1.10:8080/upstest/rest/profile/loginvalidate.sp?";
String build= null;
try {
build = Util.getProperty("login_url",getApplicationContext());
} catch (IOException e) {
e.printStackTrace();
}
String build1="username="+params[0].split(";")[0]+"&password="+params[0].split(";")[1];
String x=build+build1;
Log.e("the url ", x);
try {
URL url = new URL(x);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null)
{
Log.e("Data", "can't fetch");
}
reader = new BufferedReader(new InputStreamReader(inputStream));
Log.e("Bufferreader",String.valueOf(reader));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
// Intent intent = new Intent(getApplicationContext(), Second.class);
if (!buffer.toString().contains("failed"))
{
// startActivity(intent);
return buffer.toString();
// finish();
}
else{
return "login incorrect";
}
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:0)
只需将result==null
更改为result!=null
,因为您在登录错误条件或登录失败条件时返回"login incorrect"
,并且您正在检查result==null
,这是不正确的你正在返回任何字符串。结果不能为空。