-1

文字列データ「」をバイト形式でAndroidアプリのサーバーに送信したい。私はHttpClientを使用していますが、それは正しい方法ではないと思います。これを行う方法を教えてください。

.netの場合、Javaで同様のコードが必要です。

    string boundary = Guid.NewGuid().ToString();
    HttpWebRequest request = HttpWebRequest.Create(url)
        as HttpWebRequest;
    request.Method = "POST";
    //request.ContentType = "application/json";
    request.PreAuthenticate = true;

    byte[] fulldata = Encoding.UTF8.GetBytes(data);
    request.ContentLength = fulldata.Length;
    using (Stream sw = request.GetRequestStream())
    {
        sw.Write(fulldata, 0, fulldata.Length);
    }
4

1 に答える 1

1

最初に文字列データをバイトに変換し、ByteArrayEntity を使用してサーバーにデータをバイト形式で送信します。

このようにしてみてください

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.1/xxx");
HttpResponse response;
HttpParams hp = new BasicHttpParams();

//use ByteArrayEntity to send string data in byteformat
ByteArrayEntity byteEntity = new ByteArrayEntity(byte_data);
httppost.setEntity(byteEntity);
response = httpclient.execute(httppost); 
于 2012-12-13T13:45:42.980 に答える