将照片从android上传到rails paperclip

时间:2014-06-16 20:21:39

标签: android ruby-on-rails image upload paperclip

怎么了 虽然这似乎是一个老问题,但我不能使用任何答案! 我需要使用最后一个回形针版本将照片从android上传到rails 4。这就是我一直想做的事情:

**我知道StringBody已被弃用,但我不知道该使用什么。我也不知道我想要做的是一个不错的选择。这是我第一次这样做而且我完全迷失了。

public void onClick(View v) {

                Thread t = new Thread() {

                    public void run() {

                        Intent i = getIntent();
                        String adCategory = i.getStringExtra("categoryId");
                        String adTitle = i.getStringExtra("title");
                        String adPrice = i.getStringExtra("price");
                        String adDescription = i.getStringExtra("description");

                        int catId = Integer.parseInt(adCategory);

                        Looper.prepare();
                        HttpClient client = new DefaultHttpClient();
                        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                        HttpContext context = new BasicHttpContext();
                        HttpResponse response;
                        JSONObject json = new JSONObject();

                        try {
                            HttpPost post = new HttpPost("http://192.168.0.16:3000/ads");

                            json.put("category_id", catId);
                            json.put("title", adTitle);
                            json.put("price", adPrice);
                            json.put("description", adDescription);

                            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            bitmap.compress(CompressFormat.JPEG, 100, bos);
                            byte[] data = bos.toByteArray();
                            String file = Base64.encodeBytes(data);
                            builder.addPart("picture", new StringBody(file));
                            builder.addPart("json", new StringBody(json.toString()));
                            HttpEntity entity = builder.build();

                            post.setEntity(entity);
                            response = client.execute(post);

                            if(response!=null){
                                InputStream in = response.getEntity().getContent(); //Get the data in the entity
                            }

                        } catch(Exception e) {
                            e.printStackTrace();
                        }

                            Looper.loop();
                        }
                    };

                    t.start();      

                Intent intent = new Intent(context, NewAdActivity.class);
                startActivity(intent);
            }
        });

0 个答案:

没有答案