How to send file in JSON on android?

后端 未结 3 606
囚心锁ツ
囚心锁ツ 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:15

    You can send Text file And media file using MultiPartEntity.

    public String SendToServer(String aUrl,File Filename)
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(aUrl);
    
        try 
        {
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            entity.addPart("file", new FileBody(Filename));
            entity.addPart("video-title", new StringBody("Video"));
            entity.addPart("video-type", new StringBody("1"));
            httpPost.setEntity(entity);
    
            HttpContext context = new BasicHttpContext();
            // Bind custom cookie store to the local context
            context.setAttribute(ClientContext.COOKIE_STORE, Globals.sessionCookie);
    
            HttpResponse response = httpClient.execute(httpPost, context);
            HttpEntity resEntity = response.getEntity();  
            String Response = "";
            if (response != null) 
            {    
                Response = EntityUtils.toString(resEntity); 
            }
            return Response;
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    
        return "Exception";
    }
    

提交回复
热议问题