Skip to content

Commit

Permalink
Callback-75%
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin Wu committed Jun 16, 2017
1 parent d13304b commit 277b5e1
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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

Expand All @@ -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<String, String> 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<String, String> headers = response.getHeaders();
for (Map.Entry<String, String> 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();
}
}
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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<String, String> 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) {
Expand All @@ -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();
}


}
14 changes: 12 additions & 2 deletions app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
android:layout_height="wrap_content"
android:text="Hello World!" />
</ScrollView>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onGet"
android:text="get" />
android:onClick="onGetJSONObject"
android:text="onGetJSONObject" />

<Button
android:layout_width="wrap_content"
Expand All @@ -41,4 +45,10 @@
android:onClick="onStop"
android:text="取消所有请求" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onGetBitmap"
android:text="请求图片" />
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.github.why168.http;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
* @author Edwin.Wu
* @version 2017/6/15 22:57
* @since JDK1.8
*/
public class BitmapCallback {
public abstract class BitmapCallback extends Callback<Bitmap> {
@Override
public Bitmap parseNetworkResponse(Response response) throws Exception {
byte[] body = response.getBody();
return BitmapFactory.decodeByteArray(body, 0, body.length);
}
}
13 changes: 10 additions & 3 deletions http-library/src/main/java/com/github/why168/http/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
* @version 2017/6/13 16:18
* @since JDK1.8
*/
public interface Callback {
void onFailure(Exception e);
public abstract class Callback<T> {

void onSuccessful(String results) throws IOException;
public abstract T parseNetworkResponse(Response response) throws Exception;

public abstract void onFailure(Exception e);

public abstract void onSuccessful(Response response, T results) throws IOException;

public void onProgress(long progress, long total) {

}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.github.why168.http;

import java.io.IOException;
import java.io.File;

/**
* @author Edwin.Wu
* @version 2017/6/15 21:57
* @since JDK1.8
*/
public class FileCallback implements Callback {
public abstract class FileCallback extends Callback<File> {
@Override
public void onFailure(Exception e) {

}

@Override
public void onSuccessful(String results) throws IOException {

public File parseNetworkResponse(Response response) throws Exception {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.github.why168.http;

import java.io.IOException;
import org.json.JSONObject;

/**
* @author Edwin.Wu
* @version 2017/6/15 21:56
* @since JDK1.8
*/
public class JsonCallback implements Callback {
@Override
public void onFailure(Exception e) {

}
public abstract class JsonCallback extends Callback<JSONObject> {

@Override
public void onSuccessful(String results) throws IOException {

public JSONObject parseNetworkResponse(Response response) throws Exception {
String results = new String(response.getBody());
return new JSONObject(results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* @version 2017/6/13 16:15
* @since JDK1.8
*/
public abstract class NickRunnable implements Runnable {
abstract class NickRunnable implements Runnable {
protected final String name;

public NickRunnable(String format, Object... args) {
NickRunnable(String format, Object... args) {
this.name = String.format(Locale.US, format, args);
}

Expand Down
Loading

0 comments on commit 277b5e1

Please sign in to comment.