I am using Android Download Manager class to download file from web. The Download Manager doesn't display error by default when there is not insufficient storage. I know how to check for current storage available and compare it with current file size before downloading it with following code:
public boolean isSpaceAvailable(long bytes) {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getBlockCount();
return true;
}
But I would like to use ERROR_INSUFFICIENT_SPACE instead. How can I use this?
Android DownloadManager
I found the solution. I have Cursor to check if it moves to first and then check for `COLUMN_STATUS':
private void checkStatus(Cursor cursor){
//column for status
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
//column for reason code if the download failed or paused
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
//get the download filename
int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String filename = cursor.getString(filenameIndex);
String statusText = "";
String reasonText = "";
switch(status){
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
reasonText = "Filename:\n" + filename;
break;
}
Log.d("Error", statusText + " " + reasonText);
}
And then calling the method:
if(cursor.moveToFirst()) checkStatus(cursor);
Cite: Android Download Manager Example
Related
I want to download a zip file using download manager.Using this code it shows downloading file in notification and later shows download failed. I gave permissions such as read and write external directory.My code is as follows:
if(DownloadTask.readAndWriteExternalStorage(context)){
downloadManager=
(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse("url of zip");
DownloadManager.Request request= new
DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|
DownloadManager.Request.NETWORK_WIFI);
request.setVisibleInDownloadsUi(true);
request.setTitle("Example");
request.setDescription("Downloading a very large zip");
request.setAllowedOverRoaming(true);
request.setMimeType("df.zip");
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"df.zip");
request.setNotificationVisibility(DownloadManager.
Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference=downloadManager.enqueue(request);
}
I solved the issue with this:
DownloadManager downloadManager;
private int count = 0;
private long Zip_DownloadId,imid;
Uri uri =
Uri.parse("http://website//"+rowItems.get(i).get("Attachments"));
if ( uri.toString().endsWith(".jpg")) {
imid=DownloadData(uri, view);
Check_Image_Status(imid);
} else if (uri.toString().endsWith(".png")) {
imid=DownloadData(uri, view);
Check_Image_Status(imid);
} else if (uri.toString().endsWith(".jpeg")) {
imid=DownloadData(uri, view);
Check_Image_Status(imid);
} else if (uri.toString().endsWith(".zip")) {
Zip_DownloadId = DownloadData(uri, view);
Check_Zip_Status(Zip_DownloadId);
}
Toast.makeText(getApplicationContext(),uri.toString(),Toast.LENGTH_LONG).show();
}
});
downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//check if the broadcast message is for our Enqueued download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (referenceId == imid) {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Image Download Complete", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
} else if (referenceId == Zip_DownloadId) {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Zip Download Complete", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
}
}
};
return view;
}
private void Check_Zip_Status(long Zip_DownloadId) {
DownloadManager.Query ZipDownloadQuery = new DownloadManager.Query();
//set the query filter to our previously Enqueued download
ZipDownloadQuery.setFilterById(Zip_DownloadId);
//Query the download manager about downloads that have been requested.
Cursor cursor = downloadManager.query(ZipDownloadQuery);
if (cursor.moveToFirst()) {
DownloadStatus(cursor, Zip_DownloadId);
}}
private void Check_Image_Status(long imid) {
DownloadManager.Query ImageDownloadQuery = new DownloadManager.Query();
//set the query filter to our previously Enqueued download
ImageDownloadQuery.setFilterById(imid);
//Query the download manager about downloads that have been requested.
Cursor cursor = downloadManager.query(ImageDownloadQuery);
if(cursor.moveToFirst()){
DownloadStatus(cursor, imid);
}
}
private void DownloadStatus(Cursor cursor, long DownloadId) {
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
//column for reason code if the download failed or paused
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
//get the download filename
int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String filename = cursor.getString(filenameIndex);
String statusText = "";
String reasonText = "";
switch(status){
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
reasonText = "Filename:\n" + filename;
break;
}
if (DownloadId == Zip_DownloadId) {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Zip Download Status:" + "\n" + statusText + "\n" +
reasonText,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
} else {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Image Download Status:" + "\n" + statusText + "\n" +
reasonText,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
// Make a delay of 3 seconds so that next toast (Music Status) will not merge with this one.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, 3000);
}
}
}
private long DownloadData(Uri uri, View view) {
long downloadReference;
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path within the application's external files directory
if(uri.toString().endsWith(".jpg")) {
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.jpg");
}else if (uri.toString().endsWith(".zip")){
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.zip");
}else if(uri.toString().endsWith(".png")) {
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.png");
} else if(uri.toString().endsWith(".jpeg")) {
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.jpeg");
} //Enqueue download and save the referenceId
downloadReference = downloadManager.enqueue(request);
return downloadReference;
}
When a video is being downloaded there is a cancel option to cancel the download.but when i cancel the download i don't get any callback from DownloadManager to update the app UI.
This cancle option is only available in Android 7 .
Or else is there any option to hide or remove the cancel option from there.
After registering your broadcast receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE, you can check for DownloadManager status in the onReceive() method of the DownloadManager broadcast as follows -
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
checkDownloadStatus();
}
};
private void checkDownloadStatus(){
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
long id = preferenceManager.getLong(strPref_Download_ID, 0);
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
switch(status){
case DownloadManager.STATUS_FAILED:
String failedReason = "";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
failedReason = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
failedReason = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
failedReason = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
failedReason = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
failedReason = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
failedReason = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
failedReason = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
failedReason = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
failedReason = "ERROR_UNKNOWN";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"FAILED: " + failedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PAUSED:
String pausedReason = "";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
pausedReason = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
pausedReason = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
pausedReason = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
pausedReason = "PAUSED_WAITING_TO_RETRY";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"PAUSED: " + pausedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PENDING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"PENDING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_RUNNING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"RUNNING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(AndroidDownloadManagerActivity.this,
"SUCCESSFUL",
Toast.LENGTH_LONG).show();
downloadManager.remove(id);
break;
}
}
}
I have built app which download file from link using DownloadManager and show progress wheel while downloading. Once downloading is completed I send broadcast receiver to change progress wheel to downloaded icon.
Now facing an issue that if I cancel the downloading from notification tray I don't get any broadcast for this so the progress wheel does not stop.
Can anyone have idea how can I get broadcast for cancel downloading?
To get event of downloading process you need to register downloadManager to broadcast receiver.
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mContext.registerReceiver(downloadReceiver, filter);
Here, Broadcast receiver is :
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
checkDownloadStatus(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
}
};
So when downloading cancels or successfully downloaded or any error, you will get status. Even you cancel from notification. You can check status by:
private void checkDownloadStatus(long downloadReference) {
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
myDownloadQuery.setFilterById(downloadReference);
//Query the download manager about downloads that have been requested.
Cursor cursor = downloadManager.query(myDownloadQuery);
if (cursor.moveToFirst()) {
//column for status
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
//column for reason code if the download failed or paused
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
//get the download filename
String statusText = "";
String reasonText = "";
switch (status) {
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch (reason) {
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch (reason) {
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
break;
}
}
}
On error, you can stop your progressbar.
Is there a way to detect when the system starts a download and get information of it and force to download it to specific location? I googled it and results where not much helpful
Start this service class and then start your download it will print the result for status of download.
public class MyService extends Service {
private Timer timer;
private TimerTask timerTask;
private static final int SAMPLING_RATE = 1000;
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
#Deprecated
public void onStart(Intent intent, int startId) {
Log.e("Download", "onstart");
}
#Override
public void onCreate() {
timer = new Timer();
timerTask = new TimerTask() {
#Override
public void run() {
// Log.d(MyService.class.toString(),
// "tic ... "+System.currentTimeMillis());
getDownloadData();
}
};
if (timer != null && timerTask != null) {
timer.schedule(timerTask, 0, SAMPLING_RATE);
}
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public void getDownloadData() {
DownloadManager downloadMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_FAILED
| DownloadManager.STATUS_PENDING
| DownloadManager.STATUS_RUNNING
| DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadMgr.query(query);
if (c == null) {
System.out.println("--------------------------");
} else {
if (c.moveToFirst()) {
System.out.println("------------End--------------");
while (c.isAfterLast() == false) {
getStatus(c);
c.moveToNext();
}
}
}
}
public void getStatus(Cursor c) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int columnReason = c.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = c.getInt(columnReason);
int status = c.getInt(columnIndex);
String statusText = null;
String reasonText = null;
switch (status) {
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch (reason) {
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch (reason) {
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
break;
}
Log.d("status", statusText + " " + reasonText);
}
}
Hi i've been using ksoap for quite a while now,and all this time i was received arrays of complex objects.Now i need to send one I'm trying for a week but i can't manage it.My guess is that the complex object isn't mapped correctly on the server side.I've caught the message i'm sending with a packet sniffer.I think something's wrong with the namespaces and the mapping.Let me post you what i've done so far.The web service is wcf so I'll post you also the soap message from the wcftestclient,and also what i'm sending from my android application.
public class MainActivity extends Activity {
private Button btn;
private Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SoapObject soapObjectRequest = new SoapObject("http://microsoft.com/webservices/","InsertStudentAbsences");
SRV_Students_Absence objStudentAbsences = new SRV_Students_Absence();
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("objStudentAbsences");
propertyInfo.setType(SRV_Students_Absence.class);
propertyInfo.setValue(objStudentAbsences);
PropertyInfo propertyInfo1 = new PropertyInfo();
propertyInfo1.setName("connString");
propertyInfo1.setType(String.class);
propertyInfo1.setValue("server=verl,1524;Database=olDB;UID=sa;Password=7Esta#KAz");
soapObjectRequest.addProperty(propertyInfo1);
soapObjectRequest.addProperty(propertyInfo);
soapObjectRequest.addProperty("Company","8");
soapObjectRequest.addProperty("Lng","00");
soapObjectRequest.addProperty("MainPeriod","80");
soapObjectRequest.addProperty("UserID","14453");
soapObjectRequest.addProperty("CourseDate","2012-11-06");
SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapSerializationEnvelope.dotNet = true;
soapSerializationEnvelope.implicitTypes = true;
soapSerializationEnvelope.setOutputSoapObject(soapObjectRequest);
soapSerializationEnvelope.addMapping(soapObjectRequest.getNamespace(), "SRV_Students_Absence", objStudentAbsences.getClass());
//soapSerializationEnvelope.addMapping(soapObjectRequest.getNamespace(), "SRV_Absence_Category", SRV_Absence_Category.class);
//soapSerializationEnvelope.addMapping("http://schemas.datacontract.org/2004/07/WcfService_StudentGrades", "SRV_Student", SRV_Student.class);
try {
/**
* Mail the letter.
*/
HttpTransportSE httpTransportSE = new HttpTransportSE("http://androidwcf.schoolportal.gr");
httpTransportSE.call("http://microsoft.com/webservices/"+"IVertiSchool_SRV/InsertStudentAbsences", soapSerializationEnvelope);
httpTransportSE.debug=true;
SoapObject soapObjectResponse = (SoapObject)soapSerializationEnvelope.getResponse();
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
}
and this is the class that implemetns KvmSerializable:
public class SRV_Students_Absence implements KvmSerializable {
private int AbCode;
private String AbNum;
private int AbsCatID;
private String ActID;
private String Cause;
private int ClCode;
private int CoCode;
private String CompanyCode;
private String CourseDate;
private boolean Flag;
private String InsDate;
private int InsWay;
private String IsParentsInf;
private String IsVerified;
private String LngCode;
private String MainPeriod;
private String SmsFlag;
private int StuCode;
private int TimeCode;
private String UserIdName;
private String WPCID;
private String WPGID;
private SRV_Student objAC;
private SRV_Absence_Category objStudent;
public SRV_Students_Absence(){
AbCode= 0;
AbNum= null;
AbsCatID= 421;
ActID= null;
Cause= null;
ClCode= 375;
CoCode= 7628;
CompanyCode= "8";
CourseDate= "11/6/2012 9:50 AM";
Flag=false;
InsDate=null;
InsWay=3;
IsParentsInf=null;
IsVerified=null;
LngCode="00";
MainPeriod=null;
SmsFlag=null;
StuCode=1359;
TimeCode=1963;
UserIdName=null;
WPCID=null;
WPGID=null;
objAC=null;
objStudent=null;
}
#Override
public Object getProperty(int arg0) {
System.out.println("arxh");
switch (arg0) {
case 0:
System.out.println(">0");
return AbCode;
case 1:
System.out.println("1");
return AbNum;
case 2:
System.out.println("2");
return AbsCatID;
case 3:
System.out.println("3");
return ActID;
case 4:
System.out.println("4");
return Cause;
case 5:
System.out.println("5");
return ClCode;
case 6:
System.out.println("6");
return CoCode;
case 7:
System.out.println("7");
return CompanyCode;
case 8:
System.out.println("8");
return CourseDate;
case 9:
System.out.println("9");
return Flag;
case 10:
System.out.println("10");
return InsDate;
case 11:
System.out.println("11");
return InsWay;
case 12:
System.out.println("12");
return IsParentsInf;
case 13:
System.out.println("13");
return IsVerified;
case 14:
System.out.println("14");
return LngCode;
case 15:
System.out.println("15");
return MainPeriod;
case 16:
System.out.println("16");
return SmsFlag;
case 17:
System.out.println("17");
return StuCode;
case 18:
System.out.println("18");
return TimeCode;
case 19:
System.out.println("19");
return UserIdName;
case 20:
System.out.println("20");
return WPCID;
case 21:
System.out.println("21");
return WPGID;
case 22:
System.out.println("22");
return objAC;
case 23:
System.out.println("23");
return objStudent;
default:break;
}
return 005;
}
#Override
public int getPropertyCount() {
return 24;
}
#Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
switch (arg0) {
case 0:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "AbCode";
break;
case 1:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "AbNum";
break;
case 2:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "AbsCatID";
break;
case 3:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "ActID";
break;
case 4:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "Cause";
break;
case 5:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "ClCode";
break;
case 6:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "CoCode";
break;
case 7:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "CompanyCode";
break;
case 8:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "CourseDate";
break;
case 9:
System.out.println(":D "+arg0);
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "Flag";
break;
case 10:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "InsDate";
break;
case 11:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "InsWay";
break;
case 12:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "IsParentsInf";
break;
case 13:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "IsVerified";
break;
case 14:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "LngCode";
break;
case 15:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "MainPeriod";
break;
case 16:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "SmsFlag";
break;
case 17:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "StuCode";
break;
case 18:
System.out.println(":D "+arg0);
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "TimeCode";
break;
case 19:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserIdName";
break;
case 20:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "WPCID";
break;
case 21:
System.out.println(":D "+arg0);
info.type = PropertyInfo.STRING_CLASS;
info.name = "WPGID";
break;
case 22:
System.out.println(":D "+arg0);
info.type = SRV_Absence_Category.class;
info.name = "objAC";
break;
case 23:
System.out.println(":D "+arg0);
info.type = SRV_Student.class;
info.name = "objStudent";
break;
default:break;
}
}
#Override
public void setProperty(int arg0, Object value) {
System.out.println("setting Property");
switch (arg0) {
case 0:
AbCode = Integer.parseInt(value.toString());
case 1:
AbNum = value.toString();
case 2:
AbsCatID=(Integer.parseInt(value.toString()));
case 3:
ActID = value.toString();
case 4:
Cause = value.toString();
case 5:
ClCode = Integer.parseInt(value.toString());
case 6:
CoCode = Integer.parseInt(value.toString());
case 7:
CompanyCode= value.toString();
case 8:
CourseDate = value.toString();
case 9:
Flag = Boolean.getBoolean(value.toString());
case 10:
InsDate = value.toString();
case 11:
InsWay = Integer.parseInt(value.toString());
case 12:
IsParentsInf = value.toString();
case 13:
IsVerified = value.toString();
case 14:
LngCode = value.toString();
case 15:
MainPeriod = value.toString();
case 16:
SmsFlag = value.toString();
case 17:
StuCode = Integer.parseInt(value.toString());
case 18:
TimeCode = Integer.parseInt(value.toString());
case 19:
UserIdName = value.toString();
case 20:
WPCID = value.toString();
case 21:
WPGID= value.toString();
case 22:
objAC= null;
case 23:
objStudent= null;
}
}
}
guys I'm really stack if you have any experience in this matter give me a direction.
Thank you for your time.