使用HTTP Post时出错

时间:2011-05-03 05:02:36

标签: android http-post

使用HttpPost在Android中发送彩信时出错。

在Logcat中它说:

  

ERROR / Here(447):---------错误-----目标主机不能为空,或者在参数中设置。

我的示例代码:

String url = "myurl";
HttpClient httpClient = new DefaultHttpClient();

try {
    httpClient.getParams().setParameter(url, new Integer(90000)); // 90 second
    HttpPost post = new HttpPost(url);
    File SDCard = Environment.getExternalStorageDirectory();
    File file = new File(SDCard, "1.png");
    FileEntity entity;
    entity = new FileEntity(file,"binary/octet-stream");
    entity.setChunked(true);
    post.setEntity(entity);
    post.addHeader("Header", "UniqueName");
    Log.i("MMSHTTP","----post---------------"+post);

    HttpResponse response = httpClient.execute(post);

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        Log.e("Here",
              "--------Error--------Response Status line code:" + response.getStatusLine());
    }
    else 
    {
        // Here every thing is fine.
    }

    HttpEntity resEntity = response.getEntity();
    if (resEntity == null) {
        Log.e("Here","---------Error No Response!!-----");
    }
} catch (Exception ex) {
    Log.e("Here","---------Error-----"+ex.getMessage());
    ex.printStackTrace();
} finally {
    httpClient.getConnectionManager().shutdown();
}

如何修复错误?

1 个答案:

答案 0 :(得分:2)

您在示例代码中指定的网址是:

String url = "myurl";

为了让HttpClient能够确定主机名,您需要提供有效的URL。有点像:

String url = "http://myurl.com/index";

注意:'http://'很重要,因此可以确定适当的协议。

This guy遇到了同样的问题。