使用HttpUrlConnection在Android/Java中以二进制/八位字节的形式发布字符串数据

使用HttpUrlConnection在Android/Java中以二进制/八位字节的形式发布字符串数据,java,android,Java,Android,经过几个小时的研究,我还没有找到任何解决我的问题的方法 我有一个字符串值,我想通过POST发送它 我建造了一些东西。我只是不知道如何设置它将作为二进制/八位字节流发送 String data = someData(); String sUrl = "http://some.example.website.com"; URL url = new URL(sUrl); HttpURLConnection connection = (HttpURLConnection) (new URL(sUrl))

经过几个小时的研究,我还没有找到任何解决我的问题的方法

我有一个
字符串
值,我想通过
POST
发送它

我建造了一些东西。我只是不知道如何设置它将作为二进制/八位字节流发送

String data = someData();
String sUrl = "http://some.example.website.com";
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) (new URL(sUrl)).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
我使用的是
HttpUrlConnection
,因为我使用的
DefaultHttpClient()
已被弃用

我希望有人能帮助我


亲切的问候

您可以使用类似以下代码段的代码:

HttpClient httpclient = new DefaultHttpClient();

String responseXml = null;
StringEntity se = "test string to be sent to the server";


HttpPost httppost = new HttpPost(temp);
try {
    se.setContentType("text/soap+xml");
    httppost.setEntity(se);
    HttpResponse httpresponse = httpclient.execute(httppost);
    HttpEntity resEntity = httpresponse.getEntity();
    responseXml = EntityUtils.toString(resEntity);
    Log.d("Response XML", responseXml);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    Log.e(TAG,e.getMessage());
} catch (ClientProtocolException e) {
    Log.e(TAG,e.getMessage());
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
    Log.e(TAG, e.getMessage());
} catch (Exception e){
    e.printStackTrace();
    Log.e(TAG,e.getMessage());
}

我使用此代码向服务器发送xml。您可以替换se变量中要发送的内容。

您收到的错误是什么?。您还可以使用DefaultHttpClient(),尽管它已被弃用。如果您愿意,我可以共享我在应用程序中使用的DefaultHttpClient()代码。是的,请共享。我想用HttpUrlConnection替换DefaultHttpClient()。我看我做不到我想要的。请分享你的代码