私が作成しているアンドロイドアプリがあり、最初のインテントとしてログインページがあります。これにより、タブホルダーが開き、最初のタブにユーザーが移動して、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;
}
}
申し訳ありませんが。私が提供したコードにどこに入れますか? – iAmClownShoe