GVKun编程网logo

在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器(在使用url传值时传输的数据只能是)

15

关于在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器和在使用url传值时传输的数据只能是的问题就给大家分享到这里,感谢你花时间阅读本站内容

关于在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器在使用url传值时传输的数据只能是的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android HttpUrlConnection getInputStream引发NullPointerException、Android HttpURLConnection Post ChunkedStreamMode 上传大文件、Android HttpURLConnection.getResponseCode()错误解决方法、android – HTTPURLConnection – 使用FixedLengthStreamingMode的大文件POST多部分/表单数据等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器(在使用url传值时传输的数据只能是)

在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器(在使用url传值时传输的数据只能是)

我的服务器版本如下我的开发机器上:

Apache / 2.2.21(Win32)mod_fcgid / 2.3.6

我一直在testingHttpURLConnection因为我的项目需要简单的stream媒体function。 我已经阅读了@BalusC关于如何使用这个类的大概介绍。

使用java.net.URLConnection来触发和处理HTTP请求

如何使用batch file双击安装apk文件

在PXA270设备上运行Android OS

Android Studio – 1.5.11之前的IBus可能会导致input问题

我在哪里可以find一个很好的教程,开始使用Cocos2D-x编程?

在android shell中使用“printf”作为hex值

我目前遇到的麻烦是设置setChunkedStreamingMode 。 无论我设置到我的stream似乎并没有使服务器的数据stream是空的,当我的服务器API方法/连接被调用/作出。 但是,如果我删除它,它工作正常。

我见过另一个有类似问题的人:

Java / Android HttpURLConnection setChunkedStreamingMode不适用于所有PHP服务器

但没有真正的解决办法。 我无法简单地将它设置为setFixedLengthStreamingMode因为内容(json)的长度是可变的。

这不好。 我可能会传输大量的数据,因此不能将数据存储在内存中。

我的问题是,我怎样才能setChunkedStreamingMode发挥好? 这是服务器设置问题,还是可以在代码中解决?

编辑我已经testing了我的生产服务器上的代码,它没有任何问题。 但是我仍然想知道为什么我的本地机器上的Apache服务器失败。 任何帮助仍然非常感激。

Android支持虚拟内存吗?

软件更新选项不会出现在使用ubuntu的eclipse中

Android Studio + DDMS错误“推送select失败:只读文件系统”

从Android应用程序与服务器进行通信时出现各种HTTP错误

Android NDK套接字连接问题

尝试添加这个HTTP头:

urlConnection.setRequestHeader("transfer-encoding","chunked");

我有这样的问题:虽然我已经设置了分块的HTTP流模式(urlConnection.setChunkedStreamingMode(0)),它没有工作,但把HTTP头上面的工作正常。

我有一个类似的问题。 在我的情况下,它是安装了病毒扫描器的客户端系统。 这些扫描器有时会有拦截POST的身份盗用模块,扫描身体然后将其传递。

在传递给我之前, BitDefender缓存了大约5MB。

如果整个有效负载小于POST则以非分块固定长度请求的形式交付。

我有一个类似的问题使用HttpURLConnection 。 只需添加:

conn.setRequestProperty("connection","close"); // disables Keep Alive

到您的连接或禁用所有连接:

System.setProperty("http.keepAlive","false");

从关于disconnect()的API:

释放此连接,以便其资源可以重用或关闭。 与其他Java实现不同,这不一定会关闭可以重用的套接字连接。 您可以通过在发出任何HTTP请求之前将http.keepAlive系统属性设置为false来禁用所有连接重用。

总结

以上是小编为你收集整理的在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

Android HttpUrlConnection getInputStream引发NullPointerException

Android HttpUrlConnection getInputStream引发NullPointerException

我正在尝试从以下网址下载图像:

https://hme_player_pictures.s3.amazonaws.com/test-512813ed3b83286c72f376c7-thumb100.jpg

这是堆栈跟踪:

03-21 12:58:04.040: W/System.err(7084): java.lang.NullPointerException
03-21 12:58:04.040: W/System.err(7084):     at libcore.net.http.httpconnection$Address.hashCode(httpconnection.java:343)
03-21 12:58:04.045: W/System.err(7084):     at java.util.HashMap.get(HashMap.java:298)
03-21 12:58:04.050: W/System.err(7084):     at libcore.net.http.httpconnectionPool.get(httpconnectionPool.java:67)
03-21 12:58:04.050: W/System.err(7084):     at libcore.net.http.httpconnection.connect(httpconnection.java:128)
03-21 12:58:04.050: W/System.err(7084):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-21 12:58:04.055: W/System.err(7084):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
03-21 12:58:04.055: W/System.err(7084):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
03-21 12:58:04.055: W/System.err(7084):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-21 12:58:04.060: W/System.err(7084):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-21 12:58:04.065: W/System.err(7084):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
03-21 12:58:04.065: W/System.err(7084):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-21 12:58:04.070: W/System.err(7084):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
03-21 12:58:04.070: W/System.err(7084): ...

码:

URL imageUrl = new URL(url);
HttpURLConnection c = (HttpURLConnection)imageUrl.openConnection();
InputStream in = c.getInputStream(); // Nullpointer exception on this line, c is definitely not null, I debugged

无法弄清楚为什么会引发NullPointerException.上面的网址确实可以在浏览器中使用.

解决方法:

对于它的价值,我在Android HTTP库中看到很多错误.从条码扫描器中,我可以看到大约3500万人员的堆栈跟踪,因此,我想我已经看到了其中的一切.这是我们在下面的应用程序中发现并吞下的所有怪异内容.我建议您将其作为平台错误来解决,并优雅地失败.

https://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/HttpHelper.java

  private static int safelyConnect(String uri, HttpURLConnection connection) throws IOException {
    try {
      connection.connect();
    } catch (NullPointerException npe) {
      // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(npe.toString());
    } catch (IllegalArgumentException iae) {
      // Also seen this in the wild, not sure what to make of it. Probably a bad URL
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(iae.toString());
    } catch (SecurityException se) {
      // due to bad VPN settings?
      Log.w(TAG, "Restricted URI? " + uri);
      throw new IOException(se.toString());
    } catch (indexoutofboundsexception ioobe) {
      // Another Android problem? https://groups.google.com/forum/?fromgroups#!topic/google-admob-ads-sdk/U-WfmYa9or0
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(ioobe.toString());
    }
    try {
      return connection.getResponseCode();
    } catch (NullPointerException npe) {
      // this is maybe this Android bug: http://code.google.com/p/android/issues/detail?id=15554
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(npe.toString());
    } catch (NumberFormatException nfe) {
      // Again seen this in the wild for bad header fields in the server response!
      Log.w(TAG, "Bad server status? " + uri);
      throw new IOException(nfe.toString());
    }
  }

Android HttpURLConnection Post ChunkedStreamMode 上传大文件

Android HttpURLConnection Post ChunkedStreamMode 上传大文件

在 Android 上面我使用 HttpURLConnection 上传图片文件到 WEB 服务器,大家都知道如果不开启 StreamMode 的话,上传大文件(比如几十 M)会导致 OOM 错误,这一点我已经验证过了。但是让我苦恼的是,当我开启 StreamMode 的话,不是收到 411 Length Required 响应,就是程序报错:sendto failed: EPIPE (Broken pipe),不过也有成功的时候。在网上搜了许久未果。另外由于程序设计问题,改用其他框架 (如:Apache) 很困难,各位大牛是否有过类似的经历,求教。

Android HttpURLConnection.getResponseCode()错误解决方法

Android HttpURLConnection.getResponseCode()错误解决方法

导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下。

正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1。一直想不明白,同一个程序我调用了两次,结果有一个链接一直OK,另一个却一直报这个错误。后来发现两个链接的区别,有一个返回的内容是空的,所以导致了这个错误。

解决方法:

方法1、网页返回内容不能是空;
方法2、不要用这个接口咯。

android – HTTPURLConnection – 使用FixedLengthStreamingMode的大文件POST多部分/表单数据

android – HTTPURLConnection – 使用FixedLengthStreamingMode的大文件POST多部分/表单数据

所以我正在尝试发送带有大图像文件的multipart / form-data POST请求.我不能预先将文件转换为字节数组,我的应用程序会因OutOfMemory异常而崩溃,所以我必须将文件的内容直接写入连接的输出流.此外,我的服务器不支持分块模式,因此我必须在发送数据之前计算内容长度并使用连接的setFixedLengthStreamingMode.
public void createImagePostWithToken(String accesstoken,String text,String type,String imagePath) {

    URL imageUrl = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";

    // generating byte[] boundary here

    HttpURLConnection conn = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null; 

    int bytesRead,bytesAvailable,bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
        long contentLength;
        int serverResponseCode;
        String serverResponseMessage;
        File file = new File(imagePath);            
        FileInputStream fileInputStream = new FileInputStream(file);
        imageUrl = buildUri("posts").toURL();
        conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setDoOutput(true);
        conn.setDoInput(true);         
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);            

        String stringForLength = new String();  

        stringForLength += "Content-Type: multipart/form-data;boundary=" + boundary;

        stringForLength += twoHyphens + boundary + lineEnd + "Content-disposition: form-data; name=\"access_token\"" + lineEnd;
        stringForLength += "Content-Type: text/plain;charset=UTF-8" + lineEnd + "Content-Length: " + accesstoken.length() + lineEnd + lineEnd;
        stringForLength += accesstoken + lineEnd + twoHyphens + boundary + lineEnd;

        stringForLength += "Content-disposition: form-data; name=\"text\"" + lineEnd;
        stringForLength += "Content-Type: text/plain;charset=UTF-8" + lineEnd + "Content-Length: " + text.length() + lineEnd + lineEnd;
        stringForLength += text + lineEnd + twoHyphens + boundary + lineEnd;

        stringForLength += "Content-disposition: form-data; name=\"type\"" + lineEnd;
        stringForLength += "Content-Type: text/plain;charset=UTF-8" + lineEnd + "Content-Length: " + type.length() + lineEnd + lineEnd;
        stringForLength += type + lineEnd + twoHyphens + boundary + lineEnd;

        stringForLength += twoHyphens + boundary + lineEnd + "Content-disposition: form-data; name=\"image\"" + lineEnd;
        stringForLength += "Content-Type: application/octet-stream" + lineEnd + "Content-Length: " + file.length() + lineEnd + lineEnd;
        stringForLength += lineEnd + twoHyphens + boundary + twoHyphens + lineEnd;

        int totalLength = stringForLength.length() + (int)file.length();           
        conn.setFixedLengthStreamingMode(totalLength); 


        outputStream = new DataOutputStream( conn.getoutputStream() );          
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // access token 

        outputStream.writeBytes("Content-disposition: form-data; name=\"access_token\"" + lineEnd);
        outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        outputStream.writeBytes("Content-Length: " + accesstoken.length() + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(accesstoken + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // text 

        outputStream.writeBytes("Content-disposition: form-data; name=\"text\"" + lineEnd);
        outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        outputStream.writeBytes("Content-Length: " + text.length() + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(text + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // type 

        outputStream.writeBytes("Content-disposition: form-data; name=\"type\"" + lineEnd);
        outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        outputStream.writeBytes("Content-Length: " + type.length() + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(type + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // image

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-disposition: form-data; name=\"image\"" + lineEnd);
        //outputStream.writeBytes(lineEnd);
        outputStream.writeBytes("Content-Type: application/octet-stream" + lineEnd);
        outputStream.writeBytes("Content-Length: " + file.length() + lineEnd);
        outputStream.writeBytes(lineEnd);           

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable,maxBufferSize);
        buffer = new byte[bufferSize];
        // Read file
        bytesRead = fileInputStream.read(buffer,bufferSize);

        while (bytesRead > 0)
        {
        outputStream.write(buffer,bufferSize);          
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable,maxBufferSize);
        bytesRead = fileInputStream.read(buffer,bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        Log.d("posttemplate","connection outputstream size is " + outputStream.size());

        // finished with POST request body


     // Responses from the server (code and message)
        serverResponseCode = conn.getResponseCode();
        serverResponseMessage = conn.getResponseMessage();

        Log.d("posttemplate","server response code "+ serverResponseCode);
        Log.d("posttemplate","server response message "+ serverResponseMessage);

        fileInputStream.close();
        conn.disconnect();
        outputStream.flush();
        outputStream.close();


    } catch (MalformedURLException e)
    {
        Log.d("posttemplate","malformed url",e);
        //Todo: catch exception;
    } catch (IOException e)
    {
        Log.d("posttemplate","ioexception",e);
        //Todo: catch exception
    }        

}

不幸的是,我的应用程序在outputStream.close()时崩溃了IOException,我不明白为什么:

03-16 13:56:51.035: D/posttemplate(6479): java.io.IOException: unexpected end of stream
03-16 13:56:51.035: D/posttemplate(6479):   at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:57)
03-16 13:56:51.035: D/posttemplate(6479):   at java.io.FilterOutputStream.close(FilterOutputStream.java:66)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.api.impl.PostTemplate.createImagePostWithToken(PostTemplate.java:282)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.FutubraNewPostActivity.createPost(FutubraNewPostActivity.java:128)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.FutubraNewPostActivity_.access$2(FutubraNewPostActivity_.java:1)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.FutubraNewPostActivity_$5.run(FutubraNewPostActivity_.java:141)
03-16 13:56:51.035: D/posttemplate(6479):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-16 13:56:51.035: D/posttemplate(6479):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-16 13:56:51.035: D/posttemplate(6479):   at java.lang.Thread.run(Thread.java:1019)

解决方法

outputStream.close()之后的conn.disconnect()

我们今天的关于在HttpURLConnection中设置setChunkedStreamingMode无法将数据传递给服务器在使用url传值时传输的数据只能是的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android HttpUrlConnection getInputStream引发NullPointerException、Android HttpURLConnection Post ChunkedStreamMode 上传大文件、Android HttpURLConnection.getResponseCode()错误解决方法、android – HTTPURLConnection – 使用FixedLengthStreamingMode的大文件POST多部分/表单数据的相关信息,请在本站查询。

本文标签: