2011-11-16 74 views
0

- 稠:上传许多图像

private void postFile() { 

    HttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

    HttpPost httppost = new HttpPost(ConstValue.SEARCH_BY_PICTER); 

    File file = new File(currentPath); 

    ContentBody cbFile = new FileBody(file, "image/jpeg"); 
    MultipartEntity mpEntity = new MultipartEntity(); 

    try { 
     mpEntity.addPart("fileToUpload", cbFile); 
     mpEntity.addPart("sys", new StringBody("android")); 
     mpEntity.addPart("ver", new StringBody(ConstValue.VERSION_NAME)); 
     mpEntity.addPart("cid", new StringBody(cid)); 
     mpEntity.addPart("page", new StringBody("1")); 
     mpEntity.addPart("offset", new StringBody("80")); 
    } catch (UnsupportedEncodingException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } 

    httppost.setEntity(mpEntity); 

    // System.out.println("executing request " + httppost.getRequestLine()); 
    HttpResponse response = null; 
    try { 
     response = httpclient.execute(httppost); 
    } catch (ClientProtocolException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    HttpEntity resEntity = response.getEntity(); 


    // System.out.println(response.getStatusLine()); 
    if (resEntity != null) { 
     try { 
      final String response_str = EntityUtils.toString(resEntity); 
      if (!response_str.contains("@@@@")) { 
       ConstValue.goodByPiclist = GoodXMLUtil.parseResponse(response_str); 
      } else { 
       Toast.makeText(context, "sorry,server is busy", 2000).show(); 
      } 

      // Log.d("log", response_str); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    if (resEntity != null) { 
     try { 
      resEntity.consumeContent(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    httpclient.getConnectionManager().shutdown(); 
} 

我用罐子httpmime-4.1.2.jar,代码是相当不错,在三星is9023 Android等phone.but不运行,如果我的代码有错误,如果是的话,你可以给我一些建议或一些很好的其他method.very非常感谢你

编辑:对不起,我已经解决了这个问题,我的代码是正确的,和三星是好的,我的错误是我把图像尺寸无效

编辑:我遇到新问题,只在三星上传纯色成功为纯白色,纯黑色。其他htc android手机任何颜色即非常well.if三星is9023不信任httpmime-4.1.2.jar非常well.because在PHP中没有收到任何数据,如果我在三星其他图片

+0

“但在三星is9023没有运行,” - 什么问题?你有什么错误吗? – user370305

回答

0

一种可能性是currentPath被硬编码,如../sdcard/,并且每个设备都有不同的外部存储位置,所以请使用Environment.getExternalDirectory()

+0

感谢您的回答,我已添加代码 – pengwang

+0

所以它解决了,othrewise后erroLog和错误的细节 –