http://www.apkbus.com/plugin.php?id=apkbus_video&op=item&vid=459
视频三集教会你使用
代码太多?
我拿出一个传参方法使用
OkManager
public class OkManger {
private OkHttpClient client;
private volatile static OkManger manger;
private final String TAG = OkManger.class.getSimpleName();//获得类名
private Handler handler;
//提交json数据
private static final MediaType JSON = MediaType.parse(“application/json;charset=utf-8”);
//提交字符串
private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse(“text/x-markdown;charset=utf-8”);
private OkManger(){
client = new OkHttpClient();
handler = new Handler(Looper.getMainLooper());
}
//采用单例模式获取对象
public static OkManger getInstance(){
OkManger instance = null;
if (manger==null){
synchronized (OkManger.class){
if (instance==null){
instance = new OkManger();
manger = instance;
}
}
}
return instance;
}
public interface Func4{
void onResponse(JSONObject jsonObject);
}
/**
* 模拟表单提交
* @param url
* @param params
* @param callBack
*/
public void seendComplexForm(String url, Map<String,String> params ,final Func4 callBack){
FormBody.Builder form_builder = new FormBody.Builder(); //表单对象,包含input开始的对象,一html为主
if (params!=null&&!params.isEmpty()){
for (Map.Entry<String,String> entry :params.entrySet()){
form_builder.add(entry.getKey(),entry.getValue());
}
}
RequestBody request_body =form_builder.build();
Request request = new Request.Builder().url(url).post(request_body).build();//采用post提交
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response!=null&&response.isSuccessful()){
onSuccessJsonObjectMethod(response.body().string(),callBack);
}
}
});
}
/**
*返回响应的结果是json对象
* @param jsonValue
* @param callBack
*/
private void onSuccessJsonObjectMethod(final String jsonValue, final Func4 callBack){
handler.post(new Runnable() {
@Override
public void run() {
if (callBack!=null){
try{
callBack.onResponse(new JSONObject(jsonValue));
}catch (JSONException e){
e.printStackTrace();
}
}
}
});
}
}
Activity
private final static int SUCCESS_STATUS=0; //成功
private final static int EXIT_STATUS =1; //账号密码错误
private final static int FAIL_STATUS =999; //业务异常
private final static String TAG = LoginActivity.class.getSimpleName();
private OkManger manger; //工具类
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case SUCCESS_STATUS:
byte[] result = (byte[])msg.obj;
break;
case FAIL_STATUS:
byte[] result999 = (byte[])msg.obj;
break;
case EXIT_STATUS:
byte[] result1 = (byte[])msg.obj;
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
manger =OkManger.getInstance();
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String,String> map = new HashMap<String,String>();
map.put(“userName”,et_user.getText().toString());
map.put(“password”,et_pass.getText().toString());
// readNetLogin(AppConfig.BASE_USER_LOGIN,et_user.getText().toString(),et_pass.getText().toString());
manger.seendComplexForm(AppConfig.BASE_USER_LOGIN, map, new OkManger.Func4() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
String status = jsonObject.getString(“status”);
String msg = jsonObject.getString(“msg”);
if (status.equals(“0”)) {
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
// intent.putExtra(“token”,token);
startActivity(intent);
showToast(“登入成功”);
} else if (status.equals(“1”)) {
showToast(msg);
} else if (status.equals(“999”)) {
showToast(msg);
}
// String token = jsonObject.getString(“token”);
}catch (JSONException e){
e.printStackTrace();
}
}
});
}
});
感觉Httpclient不好用,就一直想把登入注册改为okHttpClient,接下来会进行token数据的存储实现短时间内直接登入首页,后台写好token期限,不会展示后台部分