Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b67c66

Browse files
committed
Added namespace configuration, to avoid conflicts when using the library
simultaneously in more than one app
1 parent 7d579a9 commit 7b67c66

File tree

3 files changed

+104
-117
lines changed

3 files changed

+104
-117
lines changed

README.md

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ Android Upload Service
22
======================
33

44
## Purpose
5-
I needed an easy and efficient way to upload multipart form data (HTTP parameters and files) to a server, and
5+
I needed an easy and efficient way to upload multipart form data (HTTP parameters and files) to a server, and
66
I haven't found anything useful so far that suited my needs. I also needed that the upload got handled in the
77
background and in the most efficient way on Android. More than that, I also needed to show upload status in the
88
Android Notification Center.
99

1010
So, after some research on the web I found that the best way to do this is to implement an IntentService and notify
1111
status with broadcast intents. This way the logic is decoupled from the UI and it's much more reusable. By using an
12-
IntentService, you can do multiple uploads being sure that they will be performed sequentially, and so you don't
12+
IntentService, you can do multiple uploads being sure that they will be performed sequentially, and so you don't
1313
have to deal with the nightmare of concurrency. Also, an IntentService is much more efficient than an AsyncTask, it
1414
gets executed in the background and it's completely detached from the UI thread. If you need to show status in your UI,
1515
read further and you'll discover how to do it very easily.
@@ -24,11 +24,11 @@ Add the following to your project's AndroidManifest.xml file:
2424
<uses-permission android:name="android.permission.INTERNET" />
2525
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2626
<uses-permission android:name="android.permission.WAKE_LOCK" />
27-
28-
And before the tag:
27+
28+
And before the tag:
2929

3030
</ application>
31-
31+
3232
add the following:
3333

3434
<service
@@ -39,31 +39,36 @@ add the following:
3939
<action android:name="com.alexbbb.uploadservice.action.upload"/>
4040
</intent-filter>
4141
</service>
42-
42+
43+
In your application's initialization code (for example in the onCreate method of your android.app.Application subclass), add:
44+
45+
UploadService.NAMESPACE = "com.yourcompany.yourapp";
46+
47+
4348
## Examples
4449
In the <b>examples</b> folder you will find:
4550

4651
* a demo server-side php script that handles multipart form upload
47-
* a simple demo application that uses this library
52+
* a simple demo application that uses this library
4853

4954
To be able to compile and deploy the demo application, you also need to have <b>appcompat_v7</b> library. You may need to change the path to that library in the demo application's properties.
5055

5156
## How to start android upload service to upload files
5257
For detailed explanation of each parameter, please check JavaDocs.
5358

5459
public void upload(final Context context) {
55-
final UploadRequest request = new UploadRequest(context,
56-
"custom-upload-id",
60+
final UploadRequest request = new UploadRequest(context,
61+
"custom-upload-id",
5762
"http://www.yoursite.com/yourscript");
5863

5964
/*
60-
* parameter-name: is the name of the parameter that will contain file's data.
65+
* parameter-name: is the name of the parameter that will contain file's data.
6166
* Pass "uploaded_file" if you're using the test PHP script
6267
*
63-
* custom-file-name.extension: is the file name seen by the server.
68+
* custom-file-name.extension: is the file name seen by the server.
6469
* E.g. value of $_FILES["uploaded_file"]["name"] of the test PHP script
6570
*/
66-
request.addFileToUpload("/absolute/path/to/your/file",
71+
request.addFileToUpload("/absolute/path/to/your/file",
6772
"parameter-name",
6873
"custom-file-name.extension",
6974
"content-type"));
@@ -73,16 +78,16 @@ For detailed explanation of each parameter, please check JavaDocs.
7378

7479
//and parameters
7580
request.addParameter("parameter-name", "parameter-value");
76-
81+
7782
//If you want to add a parameter with multiple values, you can do the following:
7883
request.addParameter("array-parameter-name", "value1");
7984
request.addParameter("array-parameter-name", "value2");
8085
request.addParameter("array-parameter-name", "valueN");
81-
86+
8287
//or
8388
String[] values = new String[] {"value1", "value2", "valueN"};
8489
request.addArrayParameter("array-parameter-name", values);
85-
90+
8691
//or
8792
List<String> valuesList = new ArrayList<String>();
8893
valuesList.add("value1");
@@ -97,66 +102,66 @@ For detailed explanation of each parameter, please check JavaDocs.
97102
"upload completed successfully text"
98103
"upload error text",
99104
false);
100-
105+
101106
try {
102107
//Start upload service and display the notification
103108
UploadService.startUpload(request);
104-
109+
105110
} catch (Exception exc) {
106111
//You will end up here only if you pass an incomplete UploadRequest
107112
Log.e("AndroidUploadService", exc.getLocalizedMessage(), exc);
108113
}
109114
}
110115

111116
## How to monitor upload status
112-
Once the service is started, it publishes the upload status with broadcast intents.
113-
For the sake of simplicity and to not bother you with the writing of a broadcast receiver,
117+
Once the service is started, it publishes the upload status with broadcast intents.
118+
For the sake of simplicity and to not bother you with the writing of a broadcast receiver,
114119
an abstract broadcast receiver has been implemented for you and you just need to extend it and add your custom code.
115120
So to listen for the status of the upload service in an Activity for example, you just need to do the following:
116121

117122
public class YourActivity extends Activity {
118-
123+
119124
private static final String TAG = "AndroidUploadService";
120-
125+
121126
...
122-
123-
private final AbstractUploadServiceReceiver uploadReceiver =
127+
128+
private final AbstractUploadServiceReceiver uploadReceiver =
124129
new AbstractUploadServiceReceiver() {
125130

126131
@Override
127132
public void onProgress(String uploadId, int progress) {
128-
Log.i(TAG, "The progress of the upload with ID "
133+
Log.i(TAG, "The progress of the upload with ID "
129134
+ uploadId + " is: " + progress);
130135
}
131136

132137
@Override
133138
public void onError(String uploadId, Exception exception) {
134-
Log.e(TAG, "Error in upload with ID: " + uploadId + ". "
139+
Log.e(TAG, "Error in upload with ID: " + uploadId + ". "
135140
+ exception.getLocalizedMessage(), exception);
136141
}
137142

138143
@Override
139144
public void onCompleted(String uploadId,
140-
int serverResponseCode,
145+
int serverResponseCode,
141146
String serverResponseMessage) {
142-
Log.i(TAG, "Upload with ID " + uploadId
143-
+ " is completed: " + serverResponseCode
147+
Log.i(TAG, "Upload with ID " + uploadId
148+
+ " is completed: " + serverResponseCode
144149
+ ", " + serverResponseMessage);
145150
}
146151
};
147-
152+
148153
@Override
149154
protected void onResume() {
150155
super.onResume();
151156
uploadReceiver.register(this);
152157
}
153-
158+
154159
@Override
155160
protected void onPause() {
156161
super.onPause();
157162
uploadReceiver.unregister(this);
158163
}
159-
164+
160165
}
161166

162167
If you want to monitor upload status in all of your activities, then just implement the BroadcastReceiver in your base activity class, from which all of your activities inherits and you're done.
@@ -165,14 +170,14 @@ If you want to monitor upload status in all of your activities, then just implem
165170
For security reasons, the library doesn't accept self-signed certificates by default when using HTTPS connections, but you can enable them by calling:
166171

167172
AllCertificatesAndHostsTruster.apply();
168-
173+
169174
before starting the upload service.
170175

171176
## Do you use Android Upload Service in your project?
172177
Let me know, and I'll be glad to include a link in the following list :)
173178

174179
- [VoiSmart IP Communicator](https://play.google.com/store/apps/details?id=com.voismart.softphone)
175-
180+
176181
## License
177182

178183
Copyright (C) 2013 Aleksandar Gotev

examples/app/src/com/alexbbb/uploadservice/demo/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ protected void onCreate(Bundle savedInstanceState) {
6666
super.onCreate(savedInstanceState);
6767
setContentView(R.layout.activity_main);
6868

69+
// Set your application namespace to avoid conflicts with other apps
70+
// using this library
71+
UploadService.NAMESPACE = "com.alexbbb";
72+
6973
progressBar = (ProgressBar) findViewById(R.id.uploadProgress);
7074
serverUrl = (EditText) findViewById(R.id.serverURL);
7175
fileToUpload = (EditText) findViewById(R.id.fileToUpload);

0 commit comments

Comments
 (0)