How to send file in JSON on android?

后端 未结 3 599
囚心锁ツ
囚心锁ツ 2020-12-06 08:52

I want to send files in JSON using http client I don\'t know how would I start can anyone suggest what should I need to do? I\'m going to send data on this JSON format:

3条回答
  •  借酒劲吻你
    2020-12-06 09:09

    To send Text File or Image File on Server you can use 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("responsecode" + localHttpResponse.getStatusLine().getStatusCode());
    }
    catch (Exception e)
          {
            Log.d("exception", e.toString());
          }
    

    This is working, as this code is part of my Running project.

提交回复
热议问题