2011-05-22 70 views
1

在我的應用程序中,我使用http post發送設備ID到服務器,並且我得到了會話ID。現在我需要在我的webviewclient中獲得會話cookie。我做了一些研究,發現這一點:在webview中使用來自httpclient的cookie

Android WebView Cookie Problem

的問題是該解決方案對我不起作用。我不斷收到此行的錯誤:

List<Cookie> cookies = httpClient.getCookieStore().getCookies(); 

getCookieStore()方法未定義爲HttpClient類型。我應該加載所有正確的庫,所以我不知道爲什麼我不斷收到錯誤。

這是我的代碼,也許有人可以幫助我實現一個解決方案,以獲得會話cookie到我的webview。

在此先感謝!

package mds.DragonLords; 

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.HttpResponse; 
import org.apache.http.NameValuePair; 
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; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class Home extends Activity { 


WebView mWebView; 

    private class HelloWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 

    private String tmDevice; 
    private String sid; 
    private String url; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); 


     tmDevice = "2" + tm.getDeviceId(); 

     postData(); 


     url = "myserver"+sid.substring(5); 


     setContentView(R.layout.web); 
     mWebView = (WebView) findViewById(R.id.webview); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.setWebViewClient(new HelloWebViewClient()); 
     mWebView.loadUrl(url); 
    } 

    public void postData() { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("myserver"); 
     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("uid", tmDevice)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      inputStreamToString(response.getEntity().getContent()); 



     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

    } 

    private void 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) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     sid = total.toString(); 

    } 


} 

回答