我可以成功登录并检索session_name
和session_id
但是当我执行上传任务时,响应被拒绝访问。我已经确定了权限。我发送的是uid,文件名和编码文件。我对任何帮助感到沮丧。
HttpClient httpclient = new DefaultHttpClient();
try {
//set the remote endpoint URL
HttpPost httppost = new HttpPost("http://drupal/androidrpc-endpoint/user/login");
if (resultSet.moveToFirst()) {
//if database is not empty
resultSet.moveToFirst();
String username = resultSet.getString(1);//get the username saved in the database
String password = resultSet.getString(2);//get the password saved in the database
try {
JSONObject json = new JSONObject();
//extract the username and password from UI elements and create a JSON object
json.put("username", username);
json.put("password", password);
//add serialised JSON object into POST request
StringEntity se = new StringEntity(json.toString());
//set request content type
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
//send the POST request
HttpResponse response = httpclient.execute(httppost);
//read the response from Services endpoint
String jsonResponse = EntityUtils.toString(response.getEntity());
System.out.println("The response from log in is "+ jsonResponse);//here the response is correct
JSONObject jsonObject = new JSONObject(jsonResponse);
//read the session information
session_name = jsonObject.getString("session_name");
session_id = jsonObject.getString("sessid");
log_in = true;
} catch (Exception e) {
log_in = false;
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
database.close();
String URI = "http://10.0.0.14/drupal/androidrpc-endpoint/file";
String filePath = "mnt/sdcard/application/android.png";
Bitmap bm = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
BasicHttpContext localContext = new BasicHttpContext();
CookieStore mCookieStore = new BasicCookieStore();
try {
JSONObject fileObject = new JSONObject();
HttpClient mHttpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URI);
httppost.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
fileObject.put("file", Base64.encodeToString(byteArrayImage, Base64.DEFAULT));
fileObject.put("filename", "android.PNG");
fileObject.put("uid", 1);
StringEntity se = new StringEntity(fileObject.toString());
httppost.setEntity(se);
BasicClientCookie coo = new BasicClientCookie(session_name, session_id);
coo.setVersion(0);
coo.setDomain("10.0.0.14/drupal/");
coo.setPath("/");
mCookieStore.addCookie(coo);
coo = new BasicClientCookie("has_js", "1");
mCookieStore.addCookie(coo);
localContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
HttpResponse response = mHttpClient.execute(httppost,localContext);
String logResponse = EntityUtils.toString(response.getEntity());
System.out.println("Uploading response"+ logResponse);
} catch (Exception e) {
e.printStackTrace();
}