@@ -20,30 +20,36 @@ Check out the project and add android-upload-service to your project as an [Andr
20
20
21
21
Add the following to your project's AndroidManifest.xml file:
22
22
23
-
24
- <uses-permission android:name="android.permission.INTERNET" />
25
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
26
- <uses-permission android:name="android.permission.WAKE_LOCK" />
23
+ ``` xml
24
+ <uses-permission android : name =" android.permission.INTERNET" />
25
+ <uses-permission android : name =" android.permission.READ_EXTERNAL_STORAGE" />
26
+ <uses-permission android : name =" android.permission.WAKE_LOCK" />
27
+ ```
27
28
28
29
And before the tag:
29
30
30
- </ application>
31
+ ``` xml
32
+ </ application>
33
+ ```
31
34
32
35
add the following (by changing <b >com.yourcompany.yourapp</b > to your custom app namespace):
33
36
34
- <service
35
- android:name="com.alexbbb.uploadservice.UploadService"
36
- android:enabled="true"
37
- android:exported="false" >
38
- <intent-filter>
39
- <action android:name="com.yourcompany.yourapp.uploadservice.action.upload"/>
40
- </intent-filter>
41
- </service>
37
+ ``` xml
38
+ <service
39
+ android : name =" com.alexbbb.uploadservice.UploadService"
40
+ android : enabled =" true"
41
+ android : exported =" false" >
42
+ <intent-filter >
43
+ <action android : name =" com.yourcompany.yourapp.uploadservice.action.upload" />
44
+ </intent-filter >
45
+ </service >
46
+ ```
42
47
43
48
In your application's initialization code (for example in the onCreate method of your android.app.Application subclass), add:
44
49
45
- UploadService.NAMESPACE = "com.yourcompany.yourapp";
46
-
50
+ ``` java
51
+ UploadService . NAMESPACE = " com.yourcompany.yourapp" ;
52
+ ```
47
53
48
54
## Examples
49
55
In the <b >examples</b > folder you will find:
@@ -56,124 +62,130 @@ To be able to compile and deploy the demo application, you also need to have <b>
56
62
## How to start android upload service to upload files
57
63
For detailed explanation of each parameter, please check JavaDocs.
58
64
59
- public void upload(final Context context) {
60
- final UploadRequest request = new UploadRequest( context,
61
- "custom-upload-id" ,
62
- "http://www.yoursite.com/yourscript");
63
-
64
- /*
65
- * parameter-name: is the name of the parameter that will contain file's data.
66
- * Pass "uploaded_file" if you're using the test PHP script
67
- *
68
- * custom-file-name.extension: is the file name seen by the server.
69
- * E.g. value of $_FILES["uploaded_file"][" name"] of the test PHP script
70
- */
71
- request.addFileToUpload("/absolute/path/to/your/file",
72
- "parameter-name ",
73
- "custom-file- name.extension ",
74
- "content-type"));
75
-
76
- //You can add your own custom headers
77
- request.addHeader(" your-custom-header", "your- custom-value");
78
-
79
- //and parameters
80
- request.addParameter("parameter-name", "parameter-value");
81
-
82
- //If you want to add a parameter with multiple values, you can do the following:
83
- request.addParameter("array- parameter-name", "value1");
84
- request.addParameter("array-parameter-name", "value2 ");
85
- request.addParameter("array-parameter-name", "valueN ");
86
-
87
- //or
88
- String[] values = new String[] {"value1", "value2", "valueN"};
89
- request.addArrayParameter("array-parameter-name ", values) ;
90
-
91
- //or
92
- List<String> valuesList = new ArrayList<String>();
93
- valuesList.add("value1" );
94
- valuesList.add("value2 ");
95
- valuesList.add("valueN ");
96
- request.addArrayParameter("array-parameter-name", valuesList );
97
-
98
- //configure the notification
99
- request.setNotificationConfig(android.R.drawable.ic_menu_upload,
100
- "notification title" ,
101
- "upload in progress text ",
102
- "upload completed successfully text"
103
- "upload error text",
104
- false);
105
-
106
- try {
107
- //Start upload service and display the notification
108
- UploadService.startUpload(request);
109
-
110
- } catch (Exception exc) {
111
- //You will end up here only if you pass an incomplete UploadRequest
112
- Log.e("AndroidUploadService", exc.getLocalizedMessage(), exc);
113
- }
65
+ ``` java
66
+ public void upload( final Context context) {
67
+ final UploadRequest request = new UploadRequest (context ,
68
+ " custom-upload-id " ,
69
+ " http://www.yoursite.com/yourscript " );
70
+
71
+ /*
72
+ * parameter-name: is the name of the parameter that will contain file's data.
73
+ * Pass "uploaded_file" if you're using the test PHP script
74
+ *
75
+ * custom-file-name.extension: is the file name seen by the server.
76
+ * E.g. value of $_FILES["uploaded_file"]["name"] of the test PHP script
77
+ */
78
+ request . addFileToUpload( " /absolute/path/to/your/file " ,
79
+ " parameter- name" ,
80
+ " custom-file-name.extension " ,
81
+ " content-type " ));
82
+
83
+ // You can add your own custom headers
84
+ request . addHeader( " your-custom-header " , " your-custom-value " );
85
+
86
+ // and parameters
87
+ request . addParameter( " parameter-name " , " parameter-value " );
88
+
89
+ // If you want to add a parameter with multiple values, you can do the following:
90
+ request. addParameter(" array-parameter-name" , " value1 " );
91
+ request. addParameter(" array-parameter-name" , " value2 " );
92
+ request . addParameter( " array-parameter-name " , " valueN " );
93
+
94
+ // or
95
+ String [] values = new String [] { " value1 " , " value2 " , " valueN " } ;
96
+ request . addArrayParameter( " array-parameter-name " , values);
97
+
98
+ // or
99
+ List< String > valuesList = new ArrayList< String > ( );
100
+ valuesList. add(" value1 " );
101
+ valuesList. add(" value2 " );
102
+ valuesList . add( " valueN " );
103
+ request . addArrayParameter( " array-parameter-name " , valuesList);
104
+
105
+ // configure the notification
106
+ request . setNotificationConfig( android . R . drawable . ic_menu_upload ,
107
+ " notification title " ,
108
+ " upload in progress text" ,
109
+ " upload completed successfully text"
110
+ " upload error text " ,
111
+ false );
112
+
113
+ try {
114
+ // Start upload service and display the notification
115
+ UploadService . startUpload(request);
116
+
117
+ } catch ( Exception exc) {
118
+ // You will end up here only if you pass an incomplete UploadRequest
119
+ Log . e( " AndroidUploadService " , exc . getLocalizedMessage(), exc);
114
120
}
121
+ }
122
+ ```
115
123
116
124
## How to monitor upload status
117
125
Once the service is started, it publishes the upload status with broadcast intents.
118
126
For the sake of simplicity and to not bother you with the writing of a broadcast receiver,
119
127
an abstract broadcast receiver has been implemented for you and you just need to extend it and add your custom code.
120
128
So to listen for the status of the upload service in an Activity for example, you just need to do the following:
121
129
122
- public class YourActivity extends Activity {
123
-
124
- private static final String TAG = "AndroidUploadService";
125
-
126
- ...
127
-
128
- private final AbstractUploadServiceReceiver uploadReceiver =
129
- new AbstractUploadServiceReceiver() {
130
-
131
- @Override
132
- public void onProgress(String uploadId, int progress) {
133
- Log.i(TAG, "The progress of the upload with ID "
134
- + uploadId + " is: " + progress);
135
- }
136
-
137
- @Override
138
- public void onError(String uploadId, Exception exception) {
139
- Log.e(TAG, "Error in upload with ID: " + uploadId + ". "
140
- + exception.getLocalizedMessage(), exception);
141
- }
142
-
143
- @Override
144
- public void onCompleted(String uploadId,
145
- int serverResponseCode,
146
- String serverResponseMessage) {
147
- Log.i(TAG, "Upload with ID " + uploadId
148
- + " has been completed with HTTP " + serverResponseCode
149
- + ". Response from server: " + serverResponseMessage);
150
-
151
- //If your server responds with a JSON, you can parse it
152
- //from serverResponseMessage string using a library
153
- //such as org.json (embedded in Android) or google's gson
154
- }
155
- };
130
+ ``` java
131
+ public class YourActivity extends Activity {
132
+
133
+ private static final String TAG = " AndroidUploadService" ;
134
+
135
+ ...
136
+
137
+ private final AbstractUploadServiceReceiver uploadReceiver =
138
+ new AbstractUploadServiceReceiver () {
156
139
157
140
@Override
158
- protected void onResume( ) {
159
- super.onResume();
160
- uploadReceiver.register(this );
141
+ public void onProgress ( String uploadId , int progress ) {
142
+ Log . i( TAG , " The progress of the upload with ID "
143
+ + uploadId + " is: " + progress );
161
144
}
162
145
163
146
@Override
164
- protected void onPause( ) {
165
- super.onPause();
166
- uploadReceiver.unregister(this );
147
+ public void onError ( String uploadId , Exception exception ) {
148
+ Log . e( TAG , " Error in upload with ID: " + uploadId + " . "
149
+ + exception . getLocalizedMessage(), exception );
167
150
}
168
151
152
+ @Override
153
+ public void onCompleted (String uploadId ,
154
+ int serverResponseCode ,
155
+ String serverResponseMessage ) {
156
+ Log . i(TAG , " Upload with ID " + uploadId
157
+ + " has been completed with HTTP " + serverResponseCode
158
+ + " . Response from server: " + serverResponseMessage);
159
+
160
+ // If your server responds with a JSON, you can parse it
161
+ // from serverResponseMessage string using a library
162
+ // such as org.json (embedded in Android) or google's gson
163
+ }
164
+ };
165
+
166
+ @Override
167
+ protected void onResume () {
168
+ super . onResume();
169
+ uploadReceiver. register(this );
169
170
}
170
171
172
+ @Override
173
+ protected void onPause () {
174
+ super . onPause();
175
+ uploadReceiver. unregister(this );
176
+ }
177
+
178
+ }
179
+ ```
180
+
171
181
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.
172
182
173
183
## Using HTTPS connection with self-signed certificates
174
184
For security reasons, the library doesn't accept self-signed certificates by default when using HTTPS connections, but you can enable them by calling:
175
185
176
- AllCertificatesAndHostsTruster.apply();
186
+ ``` java
187
+ AllCertificatesAndHostsTruster . apply();
188
+ ```
177
189
178
190
before starting the upload service.
179
191
0 commit comments