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

将xml文件发送到Android中的Web服务,第1张

概述您好朋友,我正在尝试通过以下代码通过我的android应用发送存储在我的SD卡中的xml文件:publicclassCartDetailsActivityextendsActivity{Filenewxmlfile=newFile(Environment.getExternalStorageDirectory()+"/"+GlobalVariables.ada_login+".xml");@Ove

您好朋友,我正在尝试通过以下代码通过我的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:

总结

以上是内存溢出为你收集整理的将xml文件发送到Android中的Web服务全部内容,希望文章能够帮你解决将xml文件发送到Android中的Web服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1087912.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存