0

我的数据是字符串类型,我想将此数据作为 xml 发送到服务器。我该怎么做?你能帮帮我吗!

httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://longvansolution.tk/login.php"); // make
                                                                            // sure
                                                                            // the
                                                                            // url
                                                                            // is
                                                                            // correct.
            // add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
            // Always use the same variable name for posting i.e the android
            // side variable name and php side variable name should be
            // similar,
            nameValuePairs.add(new BasicNameValuePair("username",
                    txtusername.getText().toString().trim())); // $Edittext_value
                                                                // =
                                                                // $_POST['Edittext_value'];
            nameValuePairs.add(new BasicNameValuePair("password",
                    txtpassword.getText().toString().trim()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            response = httpclient.execute(httppost);

现在我不想用这种方式,我想发送 xml。

4

1 回答 1

1
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
    "yourfile");
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
        new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...

} catch (Exception e) {
    // show error
}

可能重复ttp://stackoverflow.com/questions/4126625/how-to-send-a-file-in-android-from-mobile-to-server-using-http

于 2012-11-26T08:59:16.630 回答