From 277b5e164e23ceac772f42f933bac4f6732607a0 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Fri, 16 Jun 2017 15:03:30 +0800 Subject: [PATCH] Callback-75% --- .../why168/androidhttputils/HomeActivity.java | 80 +++++++++++---- app/src/main/res/layout/activity_home.xml | 14 ++- .../github/why168/http/BitmapCallback.java | 10 +- .../java/com/github/why168/http/Callback.java | 13 ++- .../com/github/why168/http/FileCallback.java | 13 +-- .../com/github/why168/http/JsonCallback.java | 13 +-- .../com/github/why168/http/NickRunnable.java | 4 +- .../java/com/github/why168/http/RealCall.java | 58 +++++++++-- .../java/com/github/why168/http/Request.java | 18 ++-- .../java/com/github/why168/http/Response.java | 98 +++++++++++++++++++ .../github/why168/http/StringCallback.java | 12 +-- 11 files changed, 267 insertions(+), 66 deletions(-) create mode 100644 http-library/src/main/java/com/github/why168/http/Response.java diff --git a/app/src/main/java/com/github/why168/androidhttputils/HomeActivity.java b/app/src/main/java/com/github/why168/androidhttputils/HomeActivity.java index e4d167c..fb7ddb4 100644 --- a/app/src/main/java/com/github/why168/androidhttputils/HomeActivity.java +++ b/app/src/main/java/com/github/why168/androidhttputils/HomeActivity.java @@ -1,17 +1,21 @@ package com.github.why168.androidhttputils; +import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; +import android.widget.ImageView; import android.widget.TextView; +import com.github.why168.http.BitmapCallback; import com.github.why168.http.Call; -import com.github.why168.http.Callback; import com.github.why168.http.HttpUtils; +import com.github.why168.http.JsonCallback; import com.github.why168.http.Request; +import com.github.why168.http.Response; +import com.github.why168.http.StringCallback; -import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; @@ -22,6 +26,7 @@ public class HomeActivity extends AppCompatActivity { private TextView tv_text; private HttpUtils goHttp; + private ImageView image; // 魂斗罗下载 http://124.193.230.12/imtt.dd.qq.com/16891/A1BFDC1BD905CEF01F3076509F920FD3.apk?mkey=59424b6446b6ee89&f=ae12&c=0&fsname=com.tencent.shootgame_1.2.33.7260_337260.apk&csr=1bbd&p=.apk @@ -31,40 +36,48 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); tv_text = (TextView) findViewById(R.id.tv_text); + image = (ImageView) findViewById(R.id.image); goHttp = new HttpUtils(); } - public void getHttp() { + public void onGetJSONObject() { Map headers = new HashMap<>(); headers.put("User-Agent", "ADX-SDK-GET"); headers.put("Content-Type", "text/html;charset=utf-8;application/octet-stream;application/json"); // http://jubi.com/api/v1/ticker?coin=mryc - Request request = new Request.Builder() + final Request request = new Request.Builder() .url("http://www.jubi.com/api/v1/ticker?coin=mryc") .method("GET") .headers(headers) .build(); Call call = goHttp.newCall(request); - call.enqueue(new Callback() { + call.enqueue(new JsonCallback() { @Override public void onFailure(Exception e) { tv_text.setText(e.toString()); } @Override - public void onSuccessful(String results) throws IOException { + public void onSuccessful(Response response, JSONObject results) throws IOException { try { - JSONObject jsonObject = new JSONObject(results); - String last = jsonObject.optString("last"); + String last = results.optString("last"); double i = Double.valueOf(last); double def = 0.43; Log.e("Edwin", "results = " + results + "\nlast = " + i + "\n" + "百分比 = " + new BigDecimal(((i - def) / def) * 100).floatValue() + "%"); - } catch (JSONException e) { + + Map headers = response.getHeaders(); + for (Map.Entry entry : headers.entrySet()) { + System.out.println("headers----" + entry.getKey() + " : " + entry.getValue()); + } + + System.out.println("Code = " + response.getCode() + "\nMessage = " + response.getMessage()); + + } catch (Exception e) { e.printStackTrace(); } } @@ -86,7 +99,7 @@ public void postHttp() { .build(); Call call = goHttp.newCall(request); - call.enqueue(new Callback() { + call.enqueue(new StringCallback() { @Override public void onFailure(Exception e) { String s = e.toString(); @@ -95,11 +108,10 @@ public void onFailure(Exception e) { } @Override - public void onSuccessful(String results) throws IOException { + public void onSuccessful(Response response, String results) throws IOException { tv_text.setText("POST_" + results); } }); - //TODO 取消单个请求 // call.cancel(); @@ -117,25 +129,50 @@ public void getFileHttp() { .headers(headers) .build(); - Call call = goHttp.newCall(request); - call.enqueue(new Callback() { + call.enqueue(new StringCallback() { @Override public void onFailure(Exception e) { tv_text.setText(e.toString()); } @Override - public void onSuccessful(String results) throws IOException { + public void onSuccessful(Response response, String results) throws IOException { tv_text.setText("GET_" + results); } }); + } + public void onGetBitmap() { + Map headers = new HashMap<>(); + headers.put("User-Agent", "ADX-SDK-GET"); + headers.put("Content-Type", "text/html;charset=utf-8;application/octet-stream;application/json"); + + // http://jubi.com/api/v1/ticker?coin=mryc + final Request request = new Request.Builder() + .url("http://f10.baidu.com/it/u=904003689,173509581&fm=76") + .method("GET") + .headers(headers) + .build(); + + Call call = goHttp.newCall(request); + call.enqueue(new BitmapCallback() { + @Override + public void onFailure(Exception e) { + System.out.println(e); + } - public void onGet(View view) { - Log.e("Edwin", "onGet"); - getHttp(); + @Override + public void onSuccessful(Response response, Bitmap results) throws IOException { + image.setImageBitmap(results); + } + }); + } + + public void onGetJSONObject(View view) { + Log.e("Edwin", "onGetJSONObject"); + onGetJSONObject(); } public void onPost(View view) { @@ -151,5 +188,12 @@ public void onGetFile(View view) { public void onStop(View view) { Log.e("Edwin", "onStop"); goHttp.getDispatcher().cancelAll(); + } + + public void onGetBitmap(View view) { + onGetBitmap(); + } + + } diff --git a/app/src/main/res/layout/activity_home.xml b/app/src/main/res/layout/activity_home.xml index 704df36..59be5b8 100644 --- a/app/src/main/res/layout/activity_home.xml +++ b/app/src/main/res/layout/activity_home.xml @@ -16,12 +16,16 @@ android:layout_height="wrap_content" android:text="Hello World!" /> +