Skip to content

Commit 95c976a

Browse files
committed
Merge pull request #100 from longbai/use_objc_error_code
统一android 错误码与objc 一致
2 parents 91f4efe + 4cde40f commit 95c976a

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
### 增加
1010
* Estat/Xstat 等性能报告
11-
* post上传进度粒度更细(小影)
11+
* post上传进度粒度更细(小影提交)
12+
* 统一android和objective C的错误码
1213

1314
## 7.0.2 (2015-01-22)
1415

library/src/androidTest/java/com/qiniu/android/HttpTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,47 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
116116
Assert.assertEquals(418, info.statusCode);
117117
Assert.assertNotNull(info.error);
118118
}
119+
120+
@SmallTest
121+
public void testPostNoDomain() throws Throwable {
122+
123+
httpManager.postData("http://no-domain.qiniu.com", "hello".getBytes(), null, null, new CompletionHandler() {
124+
@Override
125+
public void complete(ResponseInfo rinfo, JSONObject response) {
126+
Log.d("qiniutest", rinfo.toString());
127+
info = rinfo;
128+
signal.countDown();
129+
}
130+
});
131+
132+
try {
133+
signal.await(60, TimeUnit.SECONDS); // wait for callback
134+
} catch (InterruptedException e) {
135+
e.printStackTrace();
136+
}
137+
Assert.assertNull(info.reqId);
138+
Assert.assertEquals(ResponseInfo.UnknownHost, info.statusCode);
139+
}
140+
141+
@SmallTest
142+
public void testPostNoPort() throws Throwable {
143+
144+
httpManager.postData("http://up.qiniu.com:12345", "hello".getBytes(), null, null, new CompletionHandler() {
145+
@Override
146+
public void complete(ResponseInfo rinfo, JSONObject response) {
147+
Log.d("qiniutest", rinfo.toString());
148+
info = rinfo;
149+
signal.countDown();
150+
}
151+
});
152+
153+
try {
154+
signal.await(60, TimeUnit.SECONDS); // wait for callback
155+
} catch (InterruptedException e) {
156+
e.printStackTrace();
157+
}
158+
Assert.assertNull(info.reqId);
159+
Assert.assertEquals(ResponseInfo.CannotConnectToHost, info.statusCode);
160+
}
161+
119162
}

library/src/main/java/com/qiniu/android/common/Config.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.qiniu.android.common;
22

3-
/**
4-
* Created by bailong on 14/10/8.
5-
*/
3+
64
public final class Config {
75
public static final String VERSION = "7.0.3";
86

library/src/main/java/com/qiniu/android/http/ResponseHandler.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
import com.qiniu.android.utils.Dns;
1010

1111
import org.apache.http.Header;
12+
import org.apache.http.NoHttpResponseException;
13+
import org.apache.http.conn.ConnectTimeoutException;
1214
import org.json.JSONException;
1315
import org.json.JSONObject;
1416

17+
import java.io.IOException;
1518
import java.io.UnsupportedEncodingException;
19+
import java.net.SocketException;
20+
import java.net.SocketTimeoutException;
1621
import java.net.URI;
1722
import java.net.URISyntaxException;
1823
import java.net.UnknownHostException;
@@ -107,6 +112,20 @@ private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers,
107112

108113
if (statusCode == 0) {
109114
statusCode = ResponseInfo.NetworkError;
115+
String msg = error.getMessage();
116+
if (error instanceof IOException) {
117+
if (msg != null && msg.indexOf("UnknownHostException") == 0) {
118+
statusCode = ResponseInfo.UnknownHost;
119+
} else if (msg != null && msg.indexOf("Broken pipe") == 0) {
120+
statusCode = ResponseInfo.NetworkConnectionLost;
121+
} else if (error instanceof NoHttpResponseException) {
122+
statusCode = ResponseInfo.NetworkConnectionLost;
123+
} else if (error instanceof SocketTimeoutException) {
124+
statusCode = ResponseInfo.TimedOut;
125+
} else if (error instanceof ConnectTimeoutException || error instanceof SocketException) {
126+
statusCode = ResponseInfo.CannotConnectToHost;
127+
}
128+
}
110129
}
111130

112131
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, duration, err);

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public final class ResponseInfo {
1111
public static final int InvalidFile = -3;
1212
public static final int Cancelled = -2;
1313
public static final int NetworkError = -1;
14+
15+
// <-- error code copy from ios
16+
public static final int TimedOut = -1001;
17+
public static final int UnknownHost = -1003;
18+
public static final int CannotConnectToHost = -1004;
19+
public static final int NetworkConnectionLost = -1005;
20+
21+
// -->
1422
/**
1523
* 回复状态码
1624
*/
@@ -83,15 +91,19 @@ public boolean isNetworkBroken() {
8391
}
8492

8593
public boolean isServerError() {
86-
return (statusCode >= 500 && statusCode < 600 && statusCode != 579) || statusCode == 996;
94+
return (statusCode >= 500 && statusCode < 600 && statusCode != 579)
95+
|| statusCode == 996;
8796
}
8897

8998
public boolean needSwitchServer() {
90-
return isNetworkBroken() || (statusCode >= 500 && statusCode < 600 && statusCode != 579);
99+
return statusCode == NetworkError || statusCode == CannotConnectToHost
100+
|| statusCode == TimedOut || statusCode == NetworkConnectionLost
101+
|| (statusCode >= 500 && statusCode < 600 && statusCode != 579);
91102
}
92103

93104
public boolean needRetry() {
94-
return isNetworkBroken() || isServerError() || statusCode == 406 || (statusCode == 200 && error != null);
105+
return isNetworkBroken() || isServerError() || statusCode == 406
106+
|| (statusCode == 200 && error != null);
95107
}
96108

97109
public String toString() {

0 commit comments

Comments
 (0)