English 中文(简体)
Android到Drupal cookie传输
原标题:Android to Drupal cookie transfer

有人能告诉我如何将Drupal登录cookie信息从我的Android应用程序发送回我的Drupal网站吗?

以下是我在尝试中使用的代码:

HttpResponse response;
HttpClient httpClient   =   new DefaultHttpClient();
HttpPost httpPost       =   new HttpPost("http://test2.icerge.com/testpoint/node/");
**httpPost.addHeader("Cookie: " + USERPREFERENCES.getString(COOKIE_NAME, ""), " "+USERPREFERENCES.getString(COOKIE_VALUE, ""));**
Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
try{
    List<NameValuePair> nameValuePairs  =   new ArrayList<NameValuePair>();
    nameValuePairs.add( new BasicNameValuePair("node[title]", "sample node from app") );
    nameValuePairs.add( new BasicNameValuePair("node[type]", "story") );
    nameValuePairs.add( new BasicNameValuePair("node[body]", "sample app node body content") );
    httpPost.setEntity( new UrlEncodedFormEntity(nameValuePairs));

    response    =   httpClient.execute(httpPost);

    Log.i("SEEMS TO WORK", response.toString());
    Log.v("CODE", httpPost.getRequestLine().toString() + " - " + response.toString());

}catch(Exception e){
    Log.e("HTTP-ERROR: node creation", e.toString());
}

我正在使用“httpPost addHeader”行发送我的cookie,但没有。

有人能告诉我这个问题吗?

问题回答

我不认为HttpClient默认管理cookie。因此,您需要设置一个cookie存储,将其绑定到HTTP上下文,然后在POST中使用上下文,如下所示

    mHttpContext = new BasicHttpContext();
    mCookieStore = new CookieStore();       
    mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
    ...
    HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);

一旦设置好,Drupal网站在响应时发送的任何cookie都将自动包含在后续请求中。

如果你有兴趣了解Cookie商店里有哪些Cookie,你可以随时查看

List<Cookie> cookies = mCookieStore.getCookies();




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签