gpt4 book ai didi

Android http客户端协议(protocol)异常

转载 作者:行者123 更新时间:2023-11-29 21:47:19 26 4
gpt4 key购买 nike

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

@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

最佳答案

将其从未回答的问题列表中删除:


错误是

"Caused by: org.apache.http.ProtocolException: Content-Length header already present".

删除 setHeader(),您不需要它 - HttpPut 根据您包含的实体设置 Content-Length。没有理由手动完成。

关于Android http客户端协议(protocol)异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com