如何在android listview中登录网站并获取json数据?

ldfqzlk8  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(340)

我有一个安全的php登录页面网页,如果我输入我的登录详细信息,它会重定向到data.json文件。
但是如果你没有被认证,t什么都不显示?!
有人能给我演示一下如何在android中进行身份验证以从服务器获取数据的代码或示例吗?我已经试过让get_data.php what可以进入mysql并将其转换为json,但是我如何保护它,以及之后在android应用程序中输入并获取它呢?

quhf5bfb

quhf5bfb1#

要进行登录,可以使用jsoup库https://jsoup.org/

final String USER_AGENT = "\"Mozilla/5.0 (Windows NT\" +\n" +
     "\" 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2\"";
    String loginFormUrl = "https://page.com/login";
    String loginActionUrl = "https://page.com/json";
    String username = "email@gmail.com";
    String password = "XXXX";

 HashMap<String, String> cookies = new HashMap<>();  
 HashMap<String, String> formData = new HashMap<>();  

 Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET).userAgent(USER_AGENT).execute();  
 Document loginDoc = loginForm.parse(); // this is the document that contains response 

 cookies.putAll(loginForm.cookies()); // save the cookies, this will be passed on to next request  

 formData.put("commit", "Sign in");  
 formData.put("utf8", "e2 9c 93");  
 formData.put("login", username);  
 formData.put("password", password);  
 formData.put("authenticity_token", authToken);  

 Connection.Response homePage = Jsoup.connect(loginActionUrl)  
         .cookies(cookies)  
         .data(formData)  
         .method(Connection.Method.POST)  
         .userAgent(USER_AGENT)  
         .execute();  

System.out.println(homePage.parse().html());  //The json response use this to iterate and draw

相关问题