Új hozzászólás Aktív témák

  • Arcanus

    aktív tag

    válasz sutszi #1 üzenetére

    Hát, én fejlesztem, ha érdekel hogyan jelentkezek be:

    public void login(Context context, String email, String password, AuthStateListener authStateListener) {
    if (NetworkUtils.isNetworkAvailable(context)) {
    String url = PH.Api.URL_LOGIN;
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
    response -> {

    Document document = Jsoup.parse(response);
    Element page = document.body().getElementById("page");

    if (document.title().isEmpty()) {
    finishLogin(context, email, password, page.select("img[class=hidden]").first().attr("src"), authStateListener);
    } else {
    authStateListener.onError(context.getString(R.string.error_occurred_try_again));
    }
    },
    error -> {
    Util.logError(User.class, error);
    authStateListener.onError(context.getString(R.string.error_occurred_try_again));
    }
    ) {
    @Override
    public Map<String, String> getParams() throws AuthFailureError {
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put("email", email);
    headerMap.put("pass", password);
    headerMap.put("stay", "1");
    headerMap.put("no_ip_check", "1");
    headerMap.put("leave_others", "1");
    return headerMap;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<>();
    headers.put("User-agent", PHService.USER_AGENT);
    return headers;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
    if (response.statusCode == HttpsURLConnection.HTTP_OK) {
    Map<String, String> responseHeaders = response.headers;
    String rawCookie = responseHeaders.get("Set-Cookie");

    if (rawCookie != null) {
    CookieSettings cookieSettings = new CookieSettings(context);
    List<HttpCookie> httpCookies = HttpCookie.parse(rawCookie);
    for (HttpCookie httpCookie : httpCookies) {
    cookieSettings.add(httpCookie);
    }

    }
    }
    return super.parseNetworkResponse(response);
    }
    };
    Init.getInstance().addToRequestQueue(stringRequest, null);
    } else {
    authStateListener.onError(context.getString(R.string.no_internet_connection));
    }
    }

    Sajnos nem sikerült megoldani, hogy a kapott identifiert (cookie) mindig elfogadja a PH szervere, ezért trükközök. (ezzel később fogok foglalkozni jelenlegi helyzet csak ideiglenes)

    A finishLogin azért szükséges, mert miután bejelentkezel (weben) a PH átirányít, így az appban is követnem kell, hogy megkapjak minden szükséges adatot.

    Ezek után a kapott identifiert már szimplán tudom bárhol használni:

    public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<>();
    headers.put("User-agent", USER_AGENT);
    headers.put("Cookie", PH.createCookies(context));
    return headers;
    }

    Mindezt a Volley library segítségével csinálom.

    [ Szerkesztve ]

Új hozzászólás Aktív témák