Exception in executing httpclient with PUT request

129 views Asked by At

I have to upload (PUT) a multipart data which includes a PDF/JSON file, some metadata and a header with authorization, but an exception occurs. I am newbie with multipart data and android, so if anyone can point out, where am I going wrong.

private void documentUpload(String filePath, String docType, String consID, String documentRef, String docTypeNamespace, String fileType) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPut httpPut = new HttpPut(uploadURL);


    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    try{
        multipartEntity.addPart("consignmentId", new StringBody(consID));
        multipartEntity.addPart("docType", new StringBody(docType));
        multipartEntity.addPart("documentReference", new StringBody(documentRef));
        multipartEntity.addPart("docTypeNamespace", new StringBody(docTypeNamespace));
        multipartEntity.addPart("fileType", new StringBody(docType));
        multipartEntity.addPart("file", new FileBody(new File(filePath)));
    }
    catch (Exception e){
        System.out.println("Exception Here");
    }


    httpPut.setEntity(multipartEntity);
    httpPut.addHeader("authorization","bearer "+token);

    try {
        // Exception in this line
        HttpResponse response = httpclient.execute(httpPut);
        HttpEntity entity = response.getEntity();
        String s = entity.toString();
        System.out.println( s ) ;

        HttpEntity resEntity = response.getEntity();
        System.out.println( resEntity ) ;
    }
    catch (Exception e){
        System.out.println("Exception Here2");
        e.printStackTrace();

    }
}

This is my stacktrace which comes when I try executing the PUT request

2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err: android.os.NetworkOnMainThreadException
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:1154)
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:260)
2019-03-01 11:49:28.483 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:160)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at com.example.blockchain.uploadDocument.documentUpload(uploadDocument.java:199)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at com.example.blockchain.uploadDocument.onActivityResult(uploadDocument.java:133)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7454)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
2019-03-01 11:49:28.484 9526-9526/com.example.blockchain W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2019-03-01 11:49:28.485 9526-9526/com.example.blockchain W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2

There are 2 answers

0
Septian Triadi On BEST ANSWER

Your httpclient.execute() is on UIThread this will cause NetworkOnMainThreadException and UI will be locked until the execution completed. You need new different thread in before httpclient.execute() Example from your code:

Thread thread = new Thread() {
    public void run() {
        //your try catch code
    }
};
thread.start();

If you have need change something in UI you include:

runOnUiThread(() -> {
   // Your UI code ex: textView.setText("");
});
0
Mehta On

NetworkOnMainThreadException

this will happened when you call Network Operation in Main/UI thread. Reference Link

To resolve this exception, you need to call Network operation on a Background thread

1)

Thread thread = new Thread() {
    public void run() {
        //your network opetaion
    }
};
thread.start();

or

2) create an Async task and do your network operation in doInBackground() method