Android HttpPost and HttpClient Substitute

406 views Asked by At

Just a quick question. Until now I have been using HttpClient and HttpPost to post a file and some parameters to a php script on my server. However they have been deprecated. I was wondering what i could use to substitute it. I found some answers on other posts but unfortunately i didn't manage to integrate any of them with the HttpMime Library that i am using for a MultiPartEntity post.

            HttpClient httpclient = new DefaultHttpClient();        // Deprecated
        HttpPost httpost = new HttpPost(inData[0].URL);         // Deprecated

            // Request To Entity
        MultipartEntity entity = new MultipartEntity();
            // Add Parameters
        for(Request r : inData[0].Params)
            entity.addPart(r.Key, new StringBody(r.Body));
            // Add File
        entity.addPart("img", new FileBody(inData[0].FileToUpload));

        httpost.setEntity(entity);

        HttpResponse responsePOST = httpclient.execute(httpost);    // Deprecated
        HttpEntity resEntity = responsePOST.getEntity();            // Deprecated
        if (resEntity != null)
            inData[0].RequestResult.add(EntityUtils.toString(resEntity));
        else
            throw new Exception("");
        return inData[0];

    } catch (Exception e) {
        e.printStackTrace();
        inData[0].RequestResult.add("ERROR");
    }
    return inData[0];

Would anyone point me in the right direction? Thanks

1

There are 1 answers

0
Vince V. On

At my job we also used the apache libraries but we have replaced them with okHTTP.

More information about okHTTP can be found here: http://square.github.io/okhttp/

Information about uploading multipart files can be found here:

Uploading a large file in multipart using OkHttp