android - 将 xml 文件发送到 android 中的 Web 服务

标签 android xml file http-post

你好 friend 我正在尝试通过我的 android 应用程序发送一个存储在我的 sd 卡中的 xml 文件,代码如下:

    public class CartDetailsActivity extends Activity {
    File newxmlfile = new File(Environment.getExternalStorageDirectory() + "/"+GlobalVariables.ada_login+".xml");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart_details);

        Button payButton=(Button)findViewById(R.id.pay);
        payButton.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                new PostXmlClass().execute();
            }





    });
}

    public class PostXmlClass extends AsyncTask<Void, Void, Void> {

        private final ProgressDialog dialog = new ProgressDialog(
        CartDetailsActivity.this);

protected void onPreExecute() {
    this.dialog.setMessage("Loading...");
    this.dialog.setCancelable(false);
    this.dialog.show();
}
    @Override
    protected Void doInBackground(Void... params) {

        String url = "http://mywebsite.com/myfolder/mypage.php";

        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(url);

            InputStreamEntity reqEntity = new InputStreamEntity(
                    new FileInputStream(newxmlfile), -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
        } // inside the method paste your file uploading code
        return null;
    }

    protected void onPostExecute(Void result) {

        // Here if you wish to do future process for ex. move to another activity do here

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

    }
}
}

xml 文件的类型:

<?xml version='1.0' standalone='yes' ?>
<root>
  <item>
    <code>ACT358</code>
    <price>110.00</price>
    <quantity>3</quantity>
    <totalcost>330.0</totalcost>
  </item>

<item>
  <code>ACT443</code>
  <price>110.00</price>
  <quantity>2</quantity>
  <totalcost>220.0</totalcost>
</item>
</root>

我不知道这段代码有什么问题,因为它无法正常工作并且文件无法上传。任何帮助将不胜感激。

最佳答案

您可以将其添加为 NameValuePair

String xmlContent=getStringFromFile();//your method here
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("xmlData",
                        xmlContent));

并发送为

HttpPost httppost = new HttpPost(url);

httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));

或者如果你想作为一个文件发送你需要使用多部分 Ref Here:

关于android - 将 xml 文件发送到 android 中的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21473529/

相关文章:

android - 释放 key 以签署多个 Android 应用程序

java - JAXB 解码时出错

php - 尝试测试 UPS "Quantum"接口(interface) - XML 文档格式不正确

android - 使用 Gradle 风格功能时在布局 xml 中配置包名称

c - 在不同迭代中使用 fscanf/fgets 读取文件时随机崩溃

android - 使 Android RadioGroup 在 tabhost 中可滚动

java - 类名数组/列表

java - 如何确定文件已成功写入?

android - 将变量保存到文件 - Flutter

android - 如何仅在 Ubuntu 上安装 Android Studio 命令行工具