【问题标题】:Using cookies from httpclient in webview在 webview 中使用来自 httpclient 的 cookie
【发布时间】:2011-05-22 21:51:40
【问题描述】:

在我的应用程序中,我使用 http post 将设备 ID 发送到服务器,并且我正在获取会话 ID。现在我需要在我的 webviewclient 中获取会话 cookie。我做了一些研究,发现了这个:

Android WebView Cookie Problem

问题是解决方案对我不起作用。我一直在这一行收到错误:

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

HttpClient 类型的方法 getCookieStore() 未定义。我应该加载了所有正确的库,所以我不知道为什么我不断收到错误。

这是我的代码,也许有人能帮我实现一个解决方案,将会话 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();

    }


}

【问题讨论】:

    标签: cookies httpclient webviewclient


    【解决方案1】:

    我刚刚遇到了同样的事情。我知道这是一个旧帖子,但也许它会帮助其他人。

    问题在于 postData() 的声明。

    那行现在是: HttpClient httpclient = new DefaultHttpClient();

    应该是:

    DefaultHttpClient httpclient = new DefaultHttpClient(); 看到这个:http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2012-08-24
      相关资源
      最近更新 更多