2012-03-03 20 views
2

私が作成しているアンドロイドアプリがあり、最初のインテントとしてログインページがあります。これにより、タブホルダーが開き、最初のタブにユーザーが移動して、Webビューが表示されます。このウェブサイトはアプリの大きな部分を占めており、ユーザーはログインする必要があります。ウェブサイトとアプリは、ログイン情報が同じになるように、同じサーバー上で実行される同じサインインスクリプトを使用します。私は、ユーザーがwebviewに行ってもまだログインしているように、webviewの内部にアプリにログインするときに作成されたセッションを使用したいと思います。私はwebviewで再ログインするために共有設定を使ってみましたが、ワーキング。私はそれに共有の設定を渡すことができるが、それはまだ私がウェブサイトにログインしていないと私に伝えます。このトピックについての助言は非常に高く評価されます。私はすべてのクラスの間を通過することdefaultHttpRequestを作成するセッションをAndroidアプリからWebビューのウェブサイトに開いておく

Connectionクラス:

は、ここに私のコードです。ウェブサイトのページを表示します

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クラス:あなたのWebViewのインスタンスで

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; 
    } 
} 

答えて

2

、これを呼び出します。

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

、以下の方法でオーバーライドします。これは面倒ですが、Javaにはかなり新しいIMと私はトラブルこれを実装することだ場合

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

申し訳ありませんが。私が提供したコードにどこに入れますか? – iAmClownShoe

0
webView = (WebView) findViewById(R.id.webview); 
webView.clearCache(true); 
WebViewDatabase.getInstance(this).clearHttpAuthUsernamePassword(); 
webView.setWebViewClient(new PasswordWebViewClient()); 
webView.getSettings().setJavaScriptEnabled(true); 

public class PasswordWebViewClient extends WebViewClient { 

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

     //webView.loadUrl("https://www.google.com/accounts/ServiceLoginAuth? continue=http://mail.google.com/gmail&service=mail&Email=username&Passwd=password&null=Sign+in"); 

     return true; 
    } 

    public void onReceivedHttpAuthRequest (WebView view, 
     HttpAuthHandler handler, String host, String realm){ 
     System.out.println("Entered into onReceivedHttpAuthRequest"); 

     handler.proceed(username, password); 
    } 

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show(); 
    } 

} 
+0

多分私はばかだが、あなたの答えのいずれかを得ることができないようだ。私はあなたが質問に答えることによって私を助けようとしているが、誰も私に問題を解決するのに十分な情報を提供していないことを感謝します。 – iAmClownShoe

関連する問題