我正在尝试制作一个Android应用程序,其中必须使用Web API来验证登录凭据,该Web 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; } 

是否有任何更好的/更新的方法来实现我要完成的任务?

===============>>#1 票数:0

您可以使用凌空来完成这项任务。 https://www.simplifiedcoding.net/android-volley-tutorial/

  ask by Aman Verma translate from so

本文未有回复,本站智能推荐: