使用PUT请求执行httpclient时出现异常

问题描述 投票:1回答:2

我必须上传(PUT)包含PDF / JSON文件,一些元数据和带有授权的标头的多部分数据,但会发生异常。我是多部分数据和android的新手,所以如果有人可以指出,我哪里出错了。

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();

    }
}

这是我尝试执行PUT请求时出现的堆栈跟踪

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)
android put multipart multipartentity
2个回答
0
投票

您的httpclient.execute()处于UIThread状态,这将导致NetworkOnMainThreadException,并且UI将被锁定,直到执行完成。在httpclient.execute()之前你需要新的不同线程你的代码示例:

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

如果您需要在UI中更改某些内容,请包括:

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

0
投票

NetworkOnMainThreadException

当您在Main / UI线程中调用Network Operation时会发生这种情况。 Reference Link

要解决此异常,您需要在后台线程上调用网络操作

1)

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

要么

2)创建一个Async task并在doInBackground()方法中进行网络操作

© www.soinside.com 2019 - 2024. All rights reserved.