I’m trying to upload an image file from an android device to my drupal website using services module
I can log-in succesfully:
HttpParams connectionParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(connectionParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(connectionParameters, timeoutSocket);
httpClient = new DefaultHttpClient(connectionParameters);
HttpPost httpPost = new HttpPost(serverUrl+"user/login");
JSONObject json = new JSONObject();
try{
json.put("password", editText_Password.getText().toString());
json.put("username", editText_UserName.getText().toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
//Execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
int status_code = response.getStatusLine().getStatusCode();
...
...
}
catch(Exception ex)
{
}
via response object I can get session name session id , user id and many other info.
after the login , I set no session info by myself through my HttpGet object , but use the same DefaultHttpClient, I can magically retrieve a node using the following code:
HttpGet httpPost2 = new HttpGet(serverUrl+"node/537.json");
HttpResponse response2 = httpClient.execute(httpPost2);
this made me think that, httpClient object stored the session info for me automatically.
because if I dont login first or use a new HttpClient object and try to retrieve the node, I get a 401 error.
However when I try to upload an image file as follows after logging in:
httpPost = new HttpPost(serverUrl+"file/");
json = new JSONObject();
JSONObject fileObject = new JSONObject();
fileObject.put("file", photodata); //photodata is a byte[] that is set before this point
fileObject.put("filename", "myfirstfile");
fileObject.put("filepath", "sites/default/files/myfirstimage.jpg");
json.put("file", fileObject);
se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
//Execute HTTP post request
response = httpClient.execute(httpPost);
status_code = response.getStatusLine().getStatusCode();
I get 401 error although I’m logged in and using the same HttpClient object.
I also tried adding :
httpPost.setHeader("Cookie", SessionName+"="+sessionID);
which again gives me 401 error.
I’m also not sure whether I’m using the correct url, because I’m trying to use file.create method, but writing the url as “myip:myport/rest/file/create” gives wrong address.
My aim is to upload an image to a users node, so I guess after succesfully adding the file, I’ll use node.create right?
I hope someone will help me get through this.
I found when I first started doing this that most of my errors were due to not authenticating correctly.. I’m not sure your method is correct.. I know this works.
Using Drupal Services 3, I login as such and then store my session cookie into shared preferences. dataOut is a JSON object which holds the needed user login, and password information.
Once I have my session id stored.. I go about performing tasks on drupal like this.. The following code posts a node. I use the function getCookie() to grab the session cookie if it exists.. if not, then I log in, or if it’s expired, I log in. (note, you need to set the cookie expire time in your drupal settings.php file (I think that’s where it is if I remember correctly)
The getCookie() function that keeps your cookie uptodate and working..
This really should enable you to take advantage of all that Services has to offer. Make sure your endpoints are correct, and also make sure your permissions are set. I cursed for hours before I realized I had not granted perms to make nodes to users.
So once you are logged in.. To upload a file to Drupal Services I use the following code to first convert the image to byteArray.. and then to Base64.
Once you have the encodedImage. Construct a JSON Object with the keys, file (required), filename (optional, but recommended), filesize (optional) and uid (optional, the poster I presume) JSON would therefore look like this at its simplist required form {“file”:encodedImage}. Then, after making sure you have enabled the file resource on your server, POST the data to my-server/rest-endpoint/file. The response will include a fid in JSON. You can then assign this fid to the image field of a node you subsequently create using the node resource.