Java 如何使用web API验证android应用程序中的登录

Java 如何使用web API验证android应用程序中的登录,java,android,json,validation,authentication,Java,Android,Json,Validation,Authentication,我正在尝试制作一个Android应用程序,其中必须使用web API验证登录凭据,该API从SQL Server数据库获取详细信息。我必须验证登录的方式是,当用户输入“用户名”和“密码”时,点击“登录”按钮,就会点击myURL/username/password/1形式的HTTP请求,它检查验证并返回JSON响应,如{“data1”:0,“data2”:“ok”,“data3”:{“data4”:[{“data5”:2,“data6”:“somedata”,“data7”:“somedata”、

我正在尝试制作一个Android应用程序,其中必须使用web API验证登录凭据,该API从SQL Server数据库获取详细信息。我必须验证登录的方式是,当用户输入“用户名”和“密码”时,点击“登录”按钮,就会点击
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;
}
public类MainActivity扩展了AppCompatActivity
{
密码=(EditText)findViewById(R.id.editText2);
用户名=(EditText)findViewById(R.id.editText1);
登录=(按钮)findViewById(R.id.button1);
//单击登录按钮
login.setOnClickListener(新的OnClickListener()
{
公共void onClick(视图v)
{
progressBar.setVisibility(View.VISIBLE);
字符串s1=userName.getText().toString();
字符串s2=password.getText().toString();
新的ExecuteTask().execute(s1,s2);
}
});
}
类ExecuteTask扩展异步任务
{  
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串res=PostData(参数);
返回res;
}
@凌驾
受保护的void onPostExecute(字符串结果){
progressBar.setVisibility(View.GONE);
//进程设置可见性(视图已消失);
makeText(getApplicationContext(),result,3000).show();
}
}
公共字符串PostData(字符串[]值){
字符串s=“”;
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“myURL”+用户名+“/”+密码+“/1”);
setEntity(新的UrlEncodedFormEntity(列表));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
s=读取响应(httpResponse);
}
捕获(异常){}
返回s;
}
公共字符串读取响应(HttpResponse res){
InputStream=null;
字符串返回_text=“”;
尝试
{  
is=res.getEntity().getContent();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(is));
字符串行=”;
StringBuffer sb=新的StringBuffer();
而((line=bufferedReader.readLine())!=null)
{
某人附加(行);
}  
return_text=sb.toString();
}捕获(例外e)
{  
}  
返回文本;
}

有没有更好的/更新的方法来实现我的目标?

嗨,阿曼,你应该使用截击或改装来进行网络调用。这样你就更容易点击API的调用并使用它了responses@HappySingh这两个函数中的任何一个都可以解析我以JSON响应的形式获取的数据吗?