gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:12 25 4
gpt4 key购买 nike

你好 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com