我正在尝试使用HttpPost将图像发送到Web服务。问题是当我尝试发送图像返回图像未找到的错误时,但图像存在,我在图库中选择了它。
我该如何解决这个问题?
这是我如何尝试的
public Boolean insert(Usuario u, String fotoPath){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlPost.toString());
try {
File img = new File(fotoPath, ConvertStringToMD5.getMD5(u.getEmail().split("@")[0]));
httppost.addHeader("Authorization", "Basic " + BasicAuthenticationRest.getBasicAuthentication());
MultipartEntity me = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
me.addPart("nome", new StringBody(u.getNome()));
me.addPart("email", new StringBody(u.getEmail()));
me.addPart("senha", new StringBody(ConvertStringToMD5.getMD5(u.getSenha())));
me.addPart("device_tipo", new StringBody("android"));
me.addPart("device", new StringBody(AndroidReturnId.getAndroidId()));
me.addPart("uploadedfile", new FileBody(img, "image/png"));
httppost.setEntity(me);
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null){
String js = EntityUtils.toString(entity);
Log.i("JSON: ", js);
JSONObject json = new JSONObject(js);
if(json.getString("cod").equals("999")){
return true;
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
日志猫
11-26 09:53:01.945: D/ProgressBar(3673): setProgress = 0
11-26 09:53:01.945: D/ProgressBar(3673): setProgress = 0, fromUser = false
11-26 09:53:01.945: D/ProgressBar(3673): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
11-26 09:53:02.010: D/dalvikvm(3673): GC_FOR_ALLOC freed 166K, 18% free 17240K/20992K, paused 20ms, total 20ms
11-26 09:53:02.040: I/Path Foto:(3673): /storage/emulated/0/InstaSize.png
11-26 09:53:02.085: D/dalvikvm(3673): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueParser;.INSTANCE
11-26 09:53:02.085: W/dalvikvm(3673): VFY: unable to resolve static field 6632 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;
11-26 09:53:02.085: D/dalvikvm(3673): VFY: replacing opcode 0x62 at 0x001b
11-26 09:53:02.090: D/dalvikvm(3673): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueFormatter;.INSTANCE
11-26 09:53:02.090: W/dalvikvm(3673): VFY: unable to resolve static field 6626 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueFormatter;
11-26 09:53:02.090: D/dalvikvm(3673): VFY: replacing opcode 0x62 at 0x0015
11-26 09:53:02.090: D/ProgressBar(3673): updateDrawableBounds: left = 0
11-26 09:53:02.090: D/ProgressBar(3673): updateDrawableBounds: top = 0
11-26 09:53:02.090: D/ProgressBar(3673): updateDrawableBounds: right = 96
11-26 09:53:02.090: D/ProgressBar(3673): updateDrawableBounds: bottom = 96
11-26 09:53:02.205: E/ViewRootImpl(3673): sendUserActionEvent() mView == null
您可以一次发送NameValuePair
或MultipartEntity
...
当你使用HttpPost
时,你可以通过setEntity()
方法发送参数..没有像appendEntity()
或有点这样的方法......所以你必须在Once设置参数。
您不能一次发送NameValuePair
和MultipartEntity
参数。你必须选择其中之一。
因此,在您的代码中,您仅使用NameValuePair
发送参数。 您正在为图像创建MultipartEntity
参数,但您没有使用 HTTPPost
发送它......
试试这种方式..这可能会帮助你..
try {
httppost.addHeader("Authorization", "Basic " + BasicAuthenticationRest.getBasicAuthentication());
MultipartEntity me = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
me.addPart("nome", new StringBody(getNome())); // to send string params
me.addPart("email", new StringBody(u.getEmail()));
//other parameters
//creating image/file parameter to send
me.addPart("uploadedfile",new FileBody(new File(fotoPath))); // file parameter
//setting entity to HTTPPost
if(me!=null){
//avoiding NullPointerException
httppost.setEntity(new UrlEncodedFormEntity(me)); // sending MultipartEntity as entity
}
//getting response
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null){
String js = EntityUtils.toString(entity);
Log.i("JSON: ", js);
JSONObject json = new JSONObject(js);
if(json.getString("cod").equals("999")){
return true;
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}