BaseParser.java
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* HaoJiXing Teacher Q&A
* copyright(C)2013- Acorn International
*
* packeage:com.ozing.callteacher.parser.BaseParser.java
* create:2013年7月25日下午1:58:14
*/
package com.hjx.personalcenter.parser;
import android.text.TextUtils;
import com.hjx.personalcenter.exception.RemoteException;
import com.hjx.personalcenter.exception.ResponseError;
import org.json.JSONException;
import org.json.JSONObject;
/**
* @author jixiaolong<jixiaolong@chinadrtv.com>
* @code: 015261
*/
public abstract class BaseParser<T> {
public abstract T parse(String response) throws RemoteException, com.hjx.personalcenter.exception.RemoteException;
public static ResponseError getError(String response){
ResponseError error = new ResponseError();
if(TextUtils.isEmpty(response)){
error.setStatus(-1);
error.setMessage("接口空数据");
}else{
if(response.contains("status")){
//{"status":100,"message":"success","access_token":"0b129fa4-cd71-4777-89d0-215bf4652146"}
//{"status":204,"message":"wrong password"}
//{"status":200,"message":"user not exist"}
try {
JSONObject object = new JSONObject(response);
int status = object.optInt("status");
error.setStatus(status);
if(status!=0 && status!=100){
String msg = object.optString("message");
if(!TextUtils.isEmpty(msg))
error.setMessage(msg);
}
} catch (JSONException e) {
error.setMessage("返回json数据错误:["+response+"]");
}
}else if(response.contains("error")){
error.setMessage("服务器内部错误");
}else{
error.setStatus(100);
}
}
return error;
}
}