如何在Android上以JSON发送文件?

如何在Android上以JSON发送文件?,第1张

如何在Android上以JSON发送文件?

要在服务器上发送文本文件或图像文件,可以使用MultiPartEntity。

DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();      FileBody localFileBody = new FileBody(new File(this.picturePath), "image/jpg");      HttpPost localHttpPost = new HttpPost("http://website.com/path/....");      MultipartEntity localMultipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);      try      {        Log.d("picturepath", this.picturePath);        localMultipartEntity.addPart("Email", new StringBody("emailid@gmail.com"));        localMultipartEntity.addPart("password", new StringBody("password"));        localMultipartEntity.addPart("phone", new StringBody("9875......."));        localMultipartEntity.addPart("profilepicture", localFileBody);        localHttpPost.setEntity(localMultipartEntity);        HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpPost);        System.out.println("responsepre" + localHttpResponse.getStatusLine().getStatusCode());}catch (Exception e)      {        Log.d("exception", e.toString());      }

这是可行的,因为此代码是我的Running项目的一部分。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4889307.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存