[英]How to validate login in android app using web API
我正在尝试制作一个Android应用程序,其中必须使用Web API来验证登录凭据,该Web API从SQL Server数据库中获取详细信息。 我必须验证登录的方式是,当用户输入“用户名”和“密码”时,然后单击“登录”按钮的onClick,将myURL/username/password/1
形式的HTTP请求,以检查是否存在验证并返回JSON响应,例如{"data1":0,"data2":"ok","data3":{"data4": [{"data5":2,"data6":"somedata","data7":"somedata","data8":"someNumber","data9":"","data10":"somedata","data11":somedata}]}}
现在在这里,我对“ data2”感兴趣。 如果data2的响应为“ ok”,则表示凭据正确,而响应“ Invalid Login”则表示凭据不正确。 从data2
获取响应的正确方法是什么?
这是我到目前为止尝试过的-
MainActivity.java
public class MainActivity extends AppCompatActivity
{
password=(EditText) findViewById(R.id.editText2);
userName=(EditText) findViewById(R.id.editText1);
login=(Button) findViewById(R.id.button1);
//on click of login button
login.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
progressBar.setVisibility(View.VISIBLE);
String s1=userName.getText().toString();
String s2=password.getText().toString();
new ExecuteTask().execute(s1,s2);
}
});
}
class ExecuteTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params) {
String res=PostData(params);
return res;
}
@Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
//progess_msz.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), result, 3000).show();
}
}
public String PostData(String[] valuse) {
String s="";
try
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("myURL" + userName + "/" + passWord + "/1");
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse httpResponse= httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
s= readResponse(httpResponse);
}
catch(Exception exception) {}
return s;
}
public String readResponse(HttpResponse res) {
InputStream is=null;
String return_text="";
try
{
is=res.getEntity().getContent();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
String line="";
StringBuffer sb=new StringBuffer();
while ((line=bufferedReader.readLine())!=null)
{
sb.append(line);
}
return_text=sb.toString();
} catch (Exception e)
{
}
return return_text;
}
有什么更好/更新的方法可以实现我想要的目标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.