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

Skip to content

Commit 6c517d2

Browse files
committed
2 parents 60ecf22 + e5ba6a1 commit 6c517d2

File tree

1 file changed

+120
-104
lines changed

1 file changed

+120
-104
lines changed

README.md

Lines changed: 120 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,36 @@ Check out the project and add android-upload-service to your project as an [Andr
2020

2121
Add the following to your project's AndroidManifest.xml file:
2222

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+
```
2728

2829
And before the tag:
2930

30-
</ application>
31+
```xml
32+
</ application>
33+
```
3134

3235
add the following (by changing <b>com.yourcompany.yourapp</b> to your custom app namespace):
3336

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+
```
4247

4348
In your application's initialization code (for example in the onCreate method of your android.app.Application subclass), add:
4449

45-
UploadService.NAMESPACE = "com.yourcompany.yourapp";
46-
50+
```java
51+
UploadService.NAMESPACE = "com.yourcompany.yourapp";
52+
```
4753

4854
## Examples
4955
In the <b>examples</b> folder you will find:
@@ -56,120 +62,130 @@ To be able to compile and deploy the demo application, you also need to have <b>
5662
## How to start android upload service to upload files
5763
For detailed explanation of each parameter, please check JavaDocs.
5864

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);
114120
}
121+
}
122+
```
115123

116124
## How to monitor upload status
117125
Once the service is started, it publishes the upload status with broadcast intents.
118126
For the sake of simplicity and to not bother you with the writing of a broadcast receiver,
119127
an abstract broadcast receiver has been implemented for you and you just need to extend it and add your custom code.
120128
So to listen for the status of the upload service in an Activity for example, you just need to do the following:
121129

122-
public class YourActivity extends Activity {
123-
124-
private static final String TAG = "AndroidUploadService";
125-
126-
...
130+
```java
131+
public class YourActivity extends Activity {
127132

128-
private final AbstractUploadServiceReceiver uploadReceiver =
129-
new AbstractUploadServiceReceiver() {
133+
private static final String TAG = "AndroidUploadService";
130134

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-
}
135+
...
136136

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-
}
137+
private final AbstractUploadServiceReceiver uploadReceiver =
138+
new AbstractUploadServiceReceiver() {
142139

143-
@Override
144-
public void onCompleted(String uploadId,
145-
int serverResponseCode,
146-
String serverResponseMessage) {
147-
Log.i(TAG, "Upload with ID " + uploadId
148-
+ " is completed: " + serverResponseCode
149-
+ ", " + serverResponseMessage);
150-
}
151-
};
140+
@Override
141+
public void onProgress(String uploadId, int progress) {
142+
Log.i(TAG, "The progress of the upload with ID "
143+
+ uploadId + " is: " + progress);
144+
}
152145

153146
@Override
154-
protected void onResume() {
155-
super.onResume();
156-
uploadReceiver.register(this);
147+
public void onError(String uploadId, Exception exception) {
148+
Log.e(TAG, "Error in upload with ID: " + uploadId + ". "
149+
+ exception.getLocalizedMessage(), exception);
157150
}
158151

159152
@Override
160-
protected void onPause() {
161-
super.onPause();
162-
uploadReceiver.unregister(this);
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
163163
}
164+
};
164165

166+
@Override
167+
protected void onResume() {
168+
super.onResume();
169+
uploadReceiver.register(this);
165170
}
166171

172+
@Override
173+
protected void onPause() {
174+
super.onPause();
175+
uploadReceiver.unregister(this);
176+
}
177+
178+
}
179+
```
180+
167181
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.
168182

169183
## Using HTTPS connection with self-signed certificates
170184
For security reasons, the library doesn't accept self-signed certificates by default when using HTTPS connections, but you can enable them by calling:
171185

172-
AllCertificatesAndHostsTruster.apply();
186+
```java
187+
AllCertificatesAndHostsTruster.apply();
188+
```
173189

174190
before starting the upload service.
175191

@@ -180,7 +196,7 @@ Let me know, and I'll be glad to include a link in the following list :)
180196

181197
## License
182198

183-
Copyright (C) 2013 Aleksandar Gotev
199+
Copyright (C) 2013-2015 Aleksandar Gotev
184200

185201
Licensed under the Apache License, Version 2.0 (the "License");
186202
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)