在Webview中保持从Android应用到网站的会话打开 [英] Keep session open from Android app to website in a webview

查看:44
本文介绍了在Webview中保持从Android应用到网站的会话打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在创建的android应用,并且我有一个登录页面作为第一个意图.然后,这将打开一个标签夹并将用户带到第一个标签,该标签将显示一个Web视图.网站是应用程序的重要组成部分,要求用户登录.网站和应用程序使用在同一服务器上运行的相同登录脚本,因此登录信息相同.我想使用登录到Webview内部的应用程序时创建的会话,以便当用户转到Webview时,他们仍然登录.我尝试使用共享首选项重新登录Webview,但这不是在职的.我可以将共享的首选项传递给它,但是它仍然告诉我我尚未登录该网站.任何有关此主题的帮助将不胜感激.

I have an android app that I am creating and I have a log in page as the first intent. This will then open a tabholder and bring the user to the first tab which brings up a webview. The website is a big part of the app and requires the user to be logged in. The website and the app use the same sign in script that is run off the same server so the login information is the same. I want to use the session that was created when logging into the app inside of the webview so that when the user goes to the webview they are still logged in. I tried using shared preferences to re-login on the webview but that isn't working. Im able to pass the shared preferences to it but it still tells me i'm not logged in on the website. Any help on this topic would be greatly appreciated.

这是我的代码:

创建我将在所有类之间传递的defaultHttpRequest的连接类.

Connection class to create defaultHttpRequest that I will pass between all classes.

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class Connection1 {


    public static HttpClient httpclient = new DefaultHttpClient();
    private static HttpPost httpPost;

    public Connection1(){
    }

    public HttpPost getHttpPost(){
        return httpPost;
    }
    public HttpResponse getResponse() throws ClientProtocolException, IOException{
        return httpclient.execute(httpPost);
    }
    public void setEntity(UrlEncodedFormEntity entity){
        httpPost.setEntity(entity);
    }
    public void setHttpPost(String script){
        httpPost = new HttpPost(script);
    }
}

将显示网站页面的Webapp类:

Webapp class that will display the website pages:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebApp extends Activity {

    public static final String PREFS_NAME = "myPrefsFile";
    private static final String PREFS_USERNAME = "username";
    private static final String PREFS_PASSWORD = "password";

    String username = "";
    String password = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setContentView(R.layout.webapp);
        SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        username = pref.getString(PREFS_USERNAME, null);
        password = pref.getString(PREFS_PASSWORD, null);
        System.out.println(username + "-" + password);
        postLoginData();


    }

    public void postLoginData() {

        // Create a new HttpClient and Post Header
        Connection1 connection = new Connection1();
        connection
                .setHttpPost("script.php");

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            connection.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            String str = inputStreamToString(
                    connection.getResponse().getEntity().getContent())
                    .toString();

            if (str.toString().equalsIgnoreCase("success")) {
                WebView webapp = (WebView) findViewById(R.id.webView1);  
                webapp.loadUrl("URL");
                webapp.setWebViewClient(new WebViewClient());
                webapp.getSettings().setJavaScriptEnabled(true);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }
}

推荐答案

在您的WebView实例上,调用它.

On your WebView instance, call this.

webView.setHttpAuthUsernamePassword(url,"", "@"+username, password);

并重写以下方法:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url+"?name="+username+"&password="+password);
    view.loadUrl(url);
    return true;
}

这篇关于在Webview中保持从Android应用到网站的会话打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆