开发者

Open WebView with result from Http post

开发者 https://www.devze.com 2023-01-15 22:59 出处:网络
I having an issue from my HTTP Post. The code I\'m using are working (have tested to post data to a guestbook form and it worked).

I having an issue from my HTTP Post.

The code I'm using are working (have tested to post data to a guestbook form and it worked).

Now what I want. I have created two EditText forms, that holds values. I have a submit button there I post this data (like the test I wrote about before), but now I want to post it into a login.php page (that in a normal browser redirects me to the member.php page).

Although I know the forms are correctly filled in and it successfully posted on the "test" site, I wanna get the response from login.php and check if the user is successfully logged in or if it failed, if 开发者_开发技巧succeeded -> redirect me to member.php page.

All I know is this:

HttpResponse response = httpclient.execute(httppost); 

that executes the command. But how should I achieve the login check? Any further use of the response variable?


Well... your approach is not good at all. If you are going to allow user authenticate through your app, why do you want to redirect the user to a member.php page? why don't you just put the login form in a login.php file on the server and make the user browse through your site?

As user, if an app allows me to authenticate using EditTexts inside UI, I would expect to access all the content through the app instead of being redirected to a web interface.

Anyway, if you decide to continue doing it that way keep in mind that you would have to parse and process cookies manually, and inject them into the WebView (Google about the CookieManager class). That's the way how the user will really be logged-in in your web app.

Can you provide a small example of how to set it up? The stream I will get, is that a special server response for example, a successfully login?

Here you have:

public String getPostRequest(String url, String user, String pass) {
    HttpClient postClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response;

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", user));
        nameValuePairs.add(new BasicNameValuePair("pass", pass));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response = postClient.execute(httpPost);

        if(response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();  
                String result = convertStreamToString(instream);
                instream.close();
                return result; // here is a string of the result!!!
            }
        }
    } catch (Exception e) {}

    return null; // if it gets here, something wrong happens with the connection
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

How do you use it? Something like this:

String result = getPostRequest("http://yourpage.com/login.php", "the username", "his/her pass");
if( result.equals("OK") ){
    // voila!
}

I'm here supposing that you have something like this in your PHP code:

<?php
// login logic here

if( $success ){
    die("OK");
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消