Hello Friends I am trying to send an xml file stored in my sd card through my android app with the piece of code below:
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 file is of the type:
<?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>
I don't know whats wrong with this code as it is not working and file does not get uploaded. Any help will be appreciated.