表題の通り。
FileBodyで送ると、UTF8を指定してもなぜか画像のデータが文字化けして送られるので
railsからInvalid byte sequence in utf8が返ってくる。
参考:
https://groups.google.com/forum/#!topic/android-sdk-japan/zi-voNmjC6k
http://d.hatena.ne.jp/gszsato/20110701/1309532723
参考コード
https://github.com/shohei/GhFabLab/blob/master/src/com/example/ghfablab/AsyncHttpRequest.java
public void post(String url,String name, String quantity, String location, String note, String image_filepath) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); //HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost("http://192.168.1.100:3000/products"); httpPost.addHeader("content_type","image/jpeg"); try { MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8")); //MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("product[name]", new StringBody(name,Charset.forName("UTF-8"))); multipartEntity.addPart("product[quantity]", new StringBody(quantity,Charset.forName("UTF-8"))); multipartEntity.addPart("product[location]", new StringBody(location,Charset.forName("UTF-8"))); multipartEntity.addPart("product[note]", new StringBody(note,Charset.forName("UTF-8"))); File imgFile = new File(image_filepath); //FileBody fileBody = new FileBody(imgFile.getAbsoluteFile(), "image/jpeg"); //FileBody fileBody = new FileBody(imgFile, "image/jpeg","UTF-8"); //#########Filebodyは使わない################## //FileBody fileBody = new FileBody(imgFile, "application/octet-stream","UTF-8"); FileInputStream in = new FileInputStream(imgFile); //#########InputStreamBodyを使いましょう################## InputStreamBody streamBody = new InputStreamBody(in, "image/jpeg","shutter_plugin_sample.jpg"); //multipartEntity.addPart("product[photo]", fileBody); multipartEntity.addPart("product[photo]", streamBody); //UrlEncodedFormEntity ent = new UrlEncodedFormEntity((List<? extends NameValuePair>) multipartEntity,HTTP.UTF_8); httpPost.setEntity(multipartEntity); //httpPost.setEntity(ent); HttpResponse response = httpClient.execute(httpPost, localContext); } catch (IOException e) { e.printStackTrace(); } }