我有一個我正在創建的Android應用程序,並且我有一個登錄頁面作爲第一個意圖。然後這將打開一個標籤持有人,並將用戶帶到第一個標籤,它將顯示一個webview。該網站是應用程序的重要組成部分,並要求用戶登錄。網站和應用程序使用相同的登錄腳本,該腳本從同一服務器運行,因此登錄信息相同。我想使用在webview中登錄應用程序時創建的會話,以便當用戶轉到webview時他們仍然登錄。我嘗試使用共享首選項在webview上重新登錄,但這不是加工。我能夠將共享首選項傳遞給它,但它仍然告訴我我沒有登錄到網站上。任何有關這個話題的幫助將不勝感激。將會話從Android應用程序打開到webview中的網站
這裏是我的代碼:
Connection類創建defaultHttpRequest,我將所有的類之間傳遞。
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類,將顯示網站頁面:
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;
}
}
很抱歉,如果這是小麻煩,但即時通訊相當新的Java和我在實施這個麻煩。你會把它放到我提供的代碼中嗎? – iAmClownShoe 2012-03-03 04:24:45