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?
This is not necessarily a code solution to your problem, but may be helpful.
When I'm approaching problems like this I need a way to see what the request headers to and from my app look like.
I typically use Charles (great tool) http://www.charlesproxy.com/ for this. And no I don't have any connection to the company, it's just a great tool.
There's a feature in Charles called a reverse proxy and basically it allows you to bounce traffic through Charles to your drupal server and you can inspect it as it flows to and from your app.
Using charles you can sniff what a good request from your web browser looks like and then you can sniff what the requests from your android app look like. Compare the two and you can see where your app is badly shaping the request headers.
The debug phase goes like this:
Once you've got Charles set up, hit your drupal server a couple of times and inspect the structure of the request/response that you're seeing from the browser.
Then hit your service a couple of times from the android app and note the differences. Maybe the cookie isn't going through, maybe it's malformed, maybe there's something else about the headers. This will let you see what you need to shoot for in order to get Drupal to accept the requests.
SOME MORE INSTRUCTIONS -
The reverse proxy will allow you to inspect traffic coming from your phone and going to your drupal site (including headers so you should see exactly what cookies are being sent). To get the reverse proxy set up, you'll need to go to reverse proxies under the proxy menu and add a new reverse proxy - the proxy will take traffic directed at your localhost:PORTNUM and re-route it to your drupal server. It'll log communications going in both directions. Also, you can hit your drupal service a couple of times from FireFox and watch how a web app authenticates so you know what you're aiming for.
In order to see phone traffic, you'll need to be sure your phone is connected to the same network as your computer. Re-write your phone code so that it addresses its web requests to your local computer. If you've got everything configured correctly then Charles will pop up a messages asking you if it's ok to allow your phone to connect. After that you can see what's different about the headers you're sending from what the web server is sending.