Android http客户端协议异常

时间:2013-03-16 04:24:31

标签: android image http-headers httpwebrequest

我正在开发小型Android应用程序,其中我试图将简单的图像发送到我的服务器。我的问题是,当我试图添加标题内容长度时,它给了我错误http客户端协议异常。我的代码结构看起来像..

@Override
protected Void doInBackground(Void... unused) {

    HttpClient hc = new DefaultHttpClient();
    String message;
    HttpPut p = new HttpPut("https://abc.com");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 100, bos);
    byte[] data = bos.toByteArray();

    try {
        int size = data.length;
        p.setEntity(new ByteArrayEntity(data));

        p.setHeader("Content-Length", Long.toString(data.length));
        //p.setHeader("Content-Length", "0");
        //p.setHeader("Content-type", "application/json");

        HttpResponse resp = hc.execute(p);
        if (resp != null)
        {
            if (resp.getStatusLine().getStatusCode() == 204)
            {

            }
        }

        Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onProgressUpdate(Void... unsued) {

}

@Override
protected void onPostExecute(Void result) {
    try {
        if (dialog.isShowing())
            dialog.dismiss();
        Log.i("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "before exception "+result);

    } catch (Exception e) {
        Log.i("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "inside catch ");
        Toast.makeText(getApplicationContext(),"Error"+e,
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }
}

我做对了吗?如何解决这个问题呢。需要帮忙。谢谢。

我的错误跟踪看起来像

03-16 10:02:47.663: W/System.err(5804): org.apache.http.client.ClientProtocolException
03-16 10:02:47.687: W/System.err(5804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
03-16 10:02:47.687: W/System.err(5804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-16 10:02:47.687: W/System.err(5804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-16 10:02:47.687: W/System.err(5804):     at com.example.profileimage.ImageUpload$ImageUploadTask.doInBackground(ImageUpload.java:243)
03-16 10:02:47.687: W/System.err(5804):     at com.example.profileimage.ImageUpload$ImageUploadTask.doInBackground(ImageUpload.java:1)
03-16 10:02:47.687: W/System.err(5804):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-16 10:02:47.687: W/System.err(5804):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
03-16 10:02:47.687: W/System.err(5804):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-16 10:02:47.687: W/System.err(5804):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-16 10:02:47.687: W/System.err(5804):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-16 10:02:47.687: W/System.err(5804):     at java.lang.Thread.run(Thread.java:1019)
03-16 10:02:47.687: W/System.err(5804): Caused by: org.apache.http.ProtocolException: Content-Length header already present
03-16 10:02:47.695: W/System.err(5804):     at org.apache.http.protocol.RequestContent.process(RequestContent.java:70)
03-16 10:02:47.695: W/System.err(5804):     at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)
03-16 10:02:47.695: W/System.err(5804):     at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160)
03-16 10:02:47.695: W/System.err(5804):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
03-16 10:02:47.695: W/System.err(5804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-16 10:02:47.695: W/System.err(5804):     ... 10 more

0 个答案:

没有答案