1

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

Android WebView Cookie 问题

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

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();

    }


}
4

1 回答 1

0

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

问题在于 postData() 的声明。

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

It should be:

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

于 2014-11-29T21:00:05.190 回答