简体   繁体   English

使用服务将图像从android应用上传到drupal

[英]Upload image from android app to drupal using services

I am trying to upload an image from the android application to drupal. 我正在尝试将图像从android应用程序上传到drupal。 On drupal I have enabled the services module with rest server. 在drupal上,我启用了带有rest服务器的服务模块。 My endpoint is androidrpc-endpoint. 我的端点是androidrpc-endpoint。 I have loged in successfully. 我已经成功登录。 However, now I am getting the error ( ["CSRF validation failed"] of type org.json.JSONArray cannot be converted to JSONObject). 但是,现在我得到了错误(类型org.json.JSONArray的[[“ CSRF验证失败”]]无法转换为JSONObject)。 If anyone could point out the rror or give a me a tutorial to follow. 如果有人可以指出错误或给我一个教程,请遵循。

        String filePath = "mnt/sdcard/application/AboutUS.jpg";
        Bitmap bm = BitmapFactory.decodeFile(filePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] byteArrayImage = baos.toByteArray();
        String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
        HttpPost httpPost = new HttpPost("http://drupal/androidrpc-endpoint/file")
        JSONObject json = new JSONObject();
        JSONObject fileObject = new JSONObject();
        try {
            fileObject.put("file", encodedImage); 
            fileObject.put("filename", "AboutUS");
            fileObject.put("uid",1);
            fileObject.put("filepath", filePath);
            json.put("file", fileObject);

            StringEntity se = new StringEntity(json.toString());
            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());
            JSONObject jsonObject = new JSONObject(jsonResponse);//here is the error
            int fid;
            fid= jsonObject.getInt("fid");
            return null;
        }
        catch(Exception e){
            e.printStackTrace();
        }

Any help please 任何帮助请

I am assuming you already know how to set the CSRF token for login authentication, since you mention that you have been logging in successfully. 我假设您已经知道如何设置CSRF令牌进行登录身份验证,因为您提到您已经成功登录了。 That said, upon logging in, you should receive a response which contains a new token. 也就是说,登录后,您应该会收到一个包含新令牌的响应。 That is the token that you should be using for the subsequent request to your Services Endpoint. 那是您应该用于对服务端点的后续请求的令牌。

Also, if you have session authentication enabled, you should be attaching your session information as well ("session_name=sessid") 另外,如果启用了会话身份验证,则还应该附加会话信息(“ session_name = sessid”)

Lastly, I don't see why you are unable to create a JSONObject with a String as its constructor parameter. 最后,我不明白为什么您无法创建以String作为其构造函数参数的JSONObject。 Perhaps it might be useful to troubleshoot by creating your JSONObject as an empty object, and then using the .put() method to assign the String to a key, prior to using Logcat or debug to investigate its contents. 在使用Logcat或调试调查其内容之前,通过将JSONObject创建为空对象,然后使用.put()方法将String分配给键,进行故障排除可能会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM