Previously I asked a question at Android to Drupal cookie transfer about sending cookies from my Android app back to my Drupal website to which I got a very good answer. The entire idea is to enable a persistent Client-Server interaction.
I adjusted my code as was directed but I still can not get things working right. My code adjustments are below:
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpResponse response;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://testsite.com/testpoint/node/");
BasicHttpContext basicContext = new BasicHttpContext();
org.apache.http.client.CookieStore cookiestore = new BasicCookieStore();
basicContext.setAttribute(ClientContext.COOKIE_STORE, cookiestore);
basicContext.setAttribute(USERPREFERENCES.getString(COOKIE_NAME, ""), USERPREFERENCES.getString(COOKIE_VALUE, "") );
//USERPREFERENCES.getString(CO0OKIE_NAME, ""), USERPREFERENCES.getString(COOKIE_VALUE, "")
// 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));
//Execute HTTP post request
//HttpResponse
response = httpClient.execute(httpPost, basicContext);
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());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//Toast.makeText(getApplicationContext(), "Create Node thread returning!", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Testing header: Cookie: " + USERPREFERENCES.getString(COOKIE_NAME, "") +","+ USERPREFERENCES.getString(COOKIE_VALUE, ""), Toast.LENGTH_LONG).show();
}
}
Basically, my cookie name and value are stored in my shared preferences USERPREFERENCES.getString(COOKIE_NAME, "") and USERPREFERENCES.getString(COOKIE_VALUE, ""). I am trying to send that information back so that when this function executes and successfully creates a new story node on my Drupal site, the author is recognized as the user who registered and signed in from the Android app.
At present, the node is created but the user is "anonymous". Hence, I need to send back Drupal's user cookie information.
Can anyone please assist me?