Skip to content

Commit 02be4c4

Browse files
committed
Merge pull request #58 from qiniu/develop
Release 6.0.4
2 parents be624f5 + f8c15da commit 02be4c4

23 files changed

+599
-310
lines changed

.travis.yml

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
1-
language: java
1+
language: android
22
jdk: oraclejdk7
33
env:
44
matrix:
55
- ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a
6-
before_install:
7-
# Install base Android SDK
8-
- chmod +x gradlew
9-
- sudo apt-get update -qq
10-
- if [ `uname -m` = x86_64 ]; then sudo apt-get install -qq --force-yes libgd2-xpm ia32-libs ia32-libs-multiarch > /dev/null; fi
11-
- wget http://dl.google.com/android/android-sdk_r22.3-linux.tgz
12-
- tar xzf android-sdk_r22.3-linux.tgz
13-
- export ANDROID_HOME=$PWD/android-sdk-linux
14-
- export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
156

16-
# Install required components.
17-
# For a full list, run `android list sdk -a --extended`
18-
# Note that sysimg-19 downloads only ARM, because only the first license query is accepted.
19-
- echo yes | android update sdk --filter platform-tools --no-ui --force > /dev/null
20-
- echo yes | android update sdk --all --filter build-tools-19.0.0 --no-ui --force > /dev/null
21-
- echo yes | android update sdk --filter android-19 --no-ui --force > /dev/null
22-
- echo yes | android update sdk --filter sysimg-19 --no-ui --force > /dev/null
23-
- echo yes | android update sdk --filter extra-android-support --no-ui --force > /dev/null
24-
- echo yes | android update sdk --filter extra-android-m2repository --no-ui --force > /dev/null
7+
android:
8+
components:
9+
- build-tools-19.0.3
2510

26-
# Create and start emulator
11+
before_install:
2712
- echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
2813
- emulator -avd test -no-skin -no-audio -no-window &
29-
3014
before_script:
31-
- adb wait-for-device
15+
- ./ci/wait_for_emulator.sh
3216
- adb shell input keyevent 82 &
3317

34-
script:
35-
#- ./gradlew build
36-
- ./gradlew connectedInstrumentTest
18+
script: ./gradlew connectedInstrumentTest

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## CHANGE LOG
22

3+
### v6.0.4
4+
2014-07-20 issue [#58](https://github.com/qiniu/android-sdk/pull/58)
5+
6+
- [#50] 统一UA
7+
- [#53] 使用自定义的QiniuException
8+
- [#56] [#57] 多host上传重试
39

410
### v6.0.3
511
2014-07-11 issue [#49](https://github.com/qiniu/android-sdk/pull/49)

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ apply plugin: 'android'
1212

1313
android {
1414
compileSdkVersion 19
15-
buildToolsVersion '19'
15+
buildToolsVersion '19.0.3'
1616

1717
sourceSets {
1818
main {
@@ -39,4 +39,4 @@ android {
3939
compile fileTree(dir: 'libs', include: '*.jar')
4040
}
4141
}
42-
// if don't find sdk, please set the local.properties
42+
// if don't find sdk, please set the local.properties

ci/wait_for_emulator.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
bootanim=""
4+
failcounter=0
5+
until [[ "$bootanim" =~ "stopped" ]]; do
6+
bootanim=`adb -e shell getprop init.svc.bootanim 2>&1`
7+
echo "$bootanim"
8+
if [[ "$bootanim" =~ "not found" ]]; then
9+
let "failcounter += 1"
10+
if [[ $failcounter -gt 3 ]]; then
11+
echo "Failed to start emulator"
12+
exit 1
13+
fi
14+
fi
15+
sleep 1
16+
done
17+
echo "Done"

src/com/qiniu/auth/CallRet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.qiniu.auth;
22

33
import com.qiniu.utils.IOnProcess;
4+
import com.qiniu.utils.QiniuException;
45

56
public abstract class CallRet implements IOnProcess {
67
public void onInit(int flag){}
78
public abstract void onSuccess(byte[] body);
8-
public abstract void onFailure(Exception ex);
9+
public abstract void onFailure(QiniuException ex);
910
public void onProcess(long current, long total){}
1011
public void onPause(Object tag){}
1112
}

src/com/qiniu/auth/Client.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.qiniu.auth;
22

33
import android.os.AsyncTask;
4-
import com.qiniu.conf.Conf;
5-
import com.qiniu.utils.ICancel;
4+
65
import org.apache.http.Header;
76
import org.apache.http.HttpEntity;
87
import org.apache.http.HttpResponse;
@@ -24,6 +23,11 @@
2423
import java.io.IOException;
2524
import java.util.concurrent.TimeUnit;
2625

26+
27+
import com.qiniu.conf.Conf;
28+
import com.qiniu.utils.ICancel;
29+
import com.qiniu.utils.QiniuException;
30+
2731
public class Client {
2832

2933
protected HttpClient mClient;
@@ -44,10 +48,8 @@ public static ClientExecutor get(String url, CallRet ret) {
4448

4549
public ClientExecutor call(ClientExecutor client, String url, HttpEntity entity, CallRet ret) {
4650
Header header = entity.getContentType();
47-
String contentType = "application/octet-stream";
48-
if (header != null) {
49-
contentType = header.getValue();
50-
}
51+
String contentType = header == null ? "application/octet-stream" : header.getValue();
52+
5153
return call(client, url, contentType, entity, ret);
5254
}
5355

@@ -76,7 +78,7 @@ protected ClientExecutor execute(ClientExecutor client, HttpRequestBase httpRequ
7678
}
7779

7880
protected HttpResponse roundtrip(HttpRequestBase httpRequest) throws IOException {
79-
httpRequest.setHeader("User-Agent", Conf.USER_AGENT);
81+
httpRequest.setHeader("User-Agent", Conf.getUserAgent());
8082
return mClient.execute(httpRequest);
8183
}
8284

@@ -91,33 +93,49 @@ public void setup(HttpRequestBase httpRequest, CallRet ret) {
9193
public void upload(long current, long total) {
9294
publishProgress(current, total);
9395
}
94-
96+
9597
@Override
9698
protected Object doInBackground(Object... objects) {
9799
try {
98100
HttpResponse resp = roundtrip(mHttpRequest);
99101
int statusCode = resp.getStatusLine().getStatusCode();
100-
String xl = resp.getFirstHeader("X-Log").getValue();
102+
String phrase = resp.getStatusLine().getReasonPhrase();
103+
104+
Header h = resp.getFirstHeader("X-Log");
105+
String xl = h == null ? "":h.getValue();
106+
107+
h = resp.getFirstHeader("X-Reqid");
108+
String reqId = h == null ? "":h.getValue();
101109

102-
if (statusCode == 401) return new Exception("unauthorized!"); // android 2.3 will not response
103-
if (xl.contains("invalid BlockCtx")) return new Exception(xl);
110+
if (statusCode == 401) {
111+
return new QiniuException(401, reqId, phrase); // android 2.3 will not response
112+
}
104113

105114
byte[] data = EntityUtils.toByteArray(resp.getEntity());
106-
if (statusCode / 100 == 2) return data;
107-
if (data.length > 0) return new Exception(new String(data));
108-
if (xl.length() > 0) return new Exception(xl);
109-
return new Exception(resp.getStatusLine().getStatusCode() + ":" + resp.getStatusLine().getReasonPhrase());
115+
if (statusCode / 100 == 2) {
116+
return data;
117+
}
118+
119+
if (data.length > 0) {
120+
return new QiniuException(statusCode, reqId, new String(data));
121+
}
122+
if (xl.length() > 0) {
123+
return new QiniuException(statusCode, reqId, xl);
124+
}
125+
return new QiniuException(statusCode, reqId, phrase);
110126
} catch (IOException e) {
111127
e.printStackTrace();
112-
return e;
128+
return new QiniuException(QiniuException.IO, "net IOException", e);
113129
}
114130
}
115131

116132
@Override
117133
protected void onProgressUpdate(Object... values) {
118-
if (failed) return;
119-
if (values.length == 1 && values[0] instanceof Exception) {
120-
mRet.onFailure((Exception) values[0]);
134+
if (failed){
135+
return;
136+
}
137+
if (values.length == 1 && values[0] instanceof QiniuException) {
138+
mRet.onFailure((QiniuException) values[0]);
121139
failed = true;
122140
return;
123141
}
@@ -126,15 +144,17 @@ protected void onProgressUpdate(Object... values) {
126144

127145
@Override
128146
protected void onPostExecute(Object o) {
129-
if (failed) return;
130-
if (o instanceof Exception) {
131-
mRet.onFailure((Exception) o);
147+
if (failed) {
148+
return;
149+
}
150+
if (o instanceof QiniuException) {
151+
mRet.onFailure((QiniuException) o);
132152
return;
133153
}
134154
mRet.onSuccess((byte[]) o);
135155
}
136156

137-
public void onFailure(Exception ex) {
157+
public void onFailure(QiniuException ex) {
138158
publishProgress(ex);
139159
cancel(true);
140160
}

src/com/qiniu/auth/JSONObjectRet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.json.JSONException;
44
import org.json.JSONObject;
55

6+
import com.qiniu.utils.QiniuException;
7+
68
public abstract class JSONObjectRet extends CallRet {
79
public JSONObjectRet(){}
810
protected int mIdx;
@@ -17,7 +19,7 @@ public void onSuccess(byte[] body) {
1719
onSuccess(obj);
1820
} catch (JSONException e) {
1921
e.printStackTrace();
20-
onFailure(new Exception(new String(body)));
22+
onFailure(new QiniuException(QiniuException.JSON, new String(body), e));
2123
}
2224
}
2325

src/com/qiniu/conf/Conf.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.qiniu.conf;
22

33
public class Conf {
4-
public static final String USER_AGENT = "qiniu android-sdk v6.0.0";
5-
public static final String UP_HOST = "http://upload.qiniu.com";
4+
public static final String VERSION = "6.0.4";
5+
public static String UP_HOST = "http://upload.qiniu.com";
6+
public static String UP_HOST2 = "http://up.qiniu.com";
7+
8+
public static String getUserAgent() {
9+
return "QiniuAndroid/" + VERSION + " (" + android.os.Build.VERSION.RELEASE + "; " + android.os.Build.MODEL+ ")";
10+
}
611
}

src/com/qiniu/demo/MyActivity.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
import android.view.View;
88
import android.widget.Button;
99
import android.widget.TextView;
10+
11+
import org.json.JSONObject;
12+
13+
import java.util.HashMap;
14+
1015
import com.qiniu.R;
1116
import com.qiniu.auth.JSONObjectRet;
1217
import com.qiniu.io.IO;
1318
import com.qiniu.io.PutExtra;
14-
import org.json.JSONObject;
15-
16-
import java.util.HashMap;
19+
import com.qiniu.utils.QiniuException;
1720

1821
public class MyActivity extends Activity implements View.OnClickListener{
1922

@@ -84,7 +87,7 @@ public void onSuccess(JSONObject resp) {
8487
}
8588

8689
@Override
87-
public void onFailure(Exception ex) {
90+
public void onFailure(QiniuException ex) {
8891
uploading = false;
8992
hint.setText("错误: " + ex.getMessage());
9093
}

src/com/qiniu/demo/MyResumableActivity.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
import android.widget.ProgressBar;
1010
import android.widget.TextView;
1111
import android.widget.Toast;
12-
import com.qiniu.R;
13-
import com.qiniu.auth.JSONObjectRet;
14-
import com.qiniu.resumableio.PutExtra;
15-
import com.qiniu.resumableio.ResumableIO;
1612
import org.json.JSONException;
1713
import org.json.JSONObject;
1814

1915
import java.util.HashMap;
2016

17+
import com.qiniu.R;
18+
import com.qiniu.auth.JSONObjectRet;
19+
import com.qiniu.resumableio.PutExtra;
20+
import com.qiniu.resumableio.ResumableIO;
21+
import com.qiniu.utils.QiniuException;
22+
2123
public class MyResumableActivity extends Activity implements View.OnClickListener {
2224
private ProgressBar pb;
2325
private Button start;
@@ -95,7 +97,7 @@ public void onPause(Object tag) {
9597
}
9698

9799
@Override
98-
public void onFailure(Exception ex) {
100+
public void onFailure(QiniuException ex) {
99101
hint.setText(ex.getMessage());
100102
}
101103
});

0 commit comments

Comments
 (0)