我想用post方法发送图片

时间:2013-08-06 17:30:32

标签: android http-post multipartform-data

我想在网络服务器上发送图片文件,但是在运行此代码时出现错误。

hc = new DefaultHttpClient();

URI u = null;
try {
    u = new URI(params[0]);
    HttpPost hp = new HttpPost();
    hp.setURI(u);


    System.out.println("start");
    MultipartEntity mp=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    System.out.println("mul");
    FileBody fb=new FileBody(new File("akash/res/drawable-hdpi/hp.jpg"),"image/jpg");
    fb.getMediaType();
    mp.addPart("picture",fb);

    System.out.println("add picture");
    hp.setEntity(mp);
    System.out.println("set entity");

    HttpResponse hr = null;
    hr = hc.execute(hp);
    System.out.println(hr.getStatusLine().getStatusCode());

1 个答案:

答案 0 :(得分:0)

您是否已在清单中获得互联网使用许可?

<uses-permission android:name="android.permission.INTERNET" />