Skip to content

Commit

Permalink
finish request
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin Wu committed Jun 19, 2017
1 parent f795fe4 commit d81a73e
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class HomeActivity extends AppCompatActivity {
private TextView tv_text;
private HttpUtils goHttp;
private HttpUtils androidHttp;
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 @@ -47,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_home);
tv_text = (TextView) findViewById(R.id.tv_text);
image = (ImageView) findViewById(R.id.image);
goHttp = new HttpUtils();
androidHttp = new HttpUtils();

}

Expand All @@ -63,7 +63,7 @@ public void onGetJSONObject() {
.headers(headers)
.build();

Call call = goHttp.newCall(request);
Call call = androidHttp.newCall(request);
call.enqueue(new JsonCallback() {
@Override
public void onFailure(Exception e) {
Expand Down Expand Up @@ -105,10 +105,10 @@ public void postHttp() {
.url("http://dev.xiaobee1.com/api/verification/")
.method("POST")
.headers(headers)
.bodys(body.getBytes())
.body(body.getBytes())
.build();

Call call = goHttp.newCall(request);
Call call = androidHttp.newCall(request);
call.enqueue(new StringCallback() {
@Override
public void onFailure(Exception e) {
Expand Down Expand Up @@ -138,21 +138,22 @@ public void getFileHttp() {
.url("http://124.193.230.12/imtt.dd.qq.com/16891/3EC8D23CFE6DC65DCA209E5E732ADE93.apk?mkey=5947105e46b6ee89&f=6e20&c=0&fsname=com.u17.comic.phone_3.3.2.1_3320100.apk&csr=1bbd&p=.apk")
.method("GET")
.headers(headers)
.tag("tagNameA")
.build();


String path = Environment.getExternalStorageDirectory().getAbsolutePath();

Call call = goHttp.newCall(request);
call.enqueue(new FileCallback(path, System.currentTimeMillis() + ".apk") {
Call call = androidHttp.newCall(request);
call.enqueue(new FileCallback(path, System.currentTimeMillis() + "A.apk") {
@Override
public void onFailure(Exception e) {
Log.e("Edwin", e.toString());
}

@Override
public void onSuccessful(Response response, File results) throws IOException {
Log.e("Edwin",response.toString());
Log.e("Edwin", response.toString());

Log.e("Edwin", results.getAbsolutePath());

Expand All @@ -164,6 +165,31 @@ public void onProgress(long progress, long total) {
}
});

request = new Request.Builder()
.url("http://124.193.230.12/imtt.dd.qq.com/16891/3EC8D23CFE6DC65DCA209E5E732ADE93.apk?mkey=5947105e46b6ee89&f=6e20&c=0&fsname=com.u17.comic.phone_3.3.2.1_3320100.apk&csr=1bbd&p=.apk")
.method("GET")
.headers(headers)
.tag("tagNameB")
.build();

androidHttp.newCall(request).enqueue(new FileCallback(path, System.currentTimeMillis() + "B.apk") {
@Override
public void onFailure(Exception e) {
Log.e("Edwin", "B------------- " + e.toString());
}

@Override
public void onSuccessful(Response response, File results) throws IOException {
Log.e("Edwin", "B------------- " + response.toString());

Log.e("Edwin", "B------------- " + results.getAbsolutePath());
}

@Override
public void onProgress(long progress, long total) {
Log.e("Edwin", "B------------- progress = " + progress + " total = " + total);
}
});
}

public void onGetBitmap() {
Expand All @@ -178,7 +204,7 @@ public void onGetBitmap() {
.headers(headers)
.build();

Call call = goHttp.newCall(request);
Call call = androidHttp.newCall(request);
call.enqueue(new BitmapCallback() {
@Override
public void onFailure(Exception e) {
Expand Down Expand Up @@ -208,7 +234,18 @@ public void onGetFile(View view) {

public void onStop(View view) {
Log.e("Edwin", "onStop");
goHttp.getDispatcher().cancelAll();
androidHttp.getDispatcher().cancelAll();
}

public void onStopA(View view) {
Log.e("Edwin", "onStopA");
androidHttp.getDispatcher().cancelWithTAG("tagNameA");

}

public void onStopB(View view) {
Log.e("Edwin", "onStopB");
androidHttp.getDispatcher().cancelWithTAG("tagNameB");

}

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
android:layout_height="wrap_content"
android:text="Hello World!" />
</ScrollView>

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -45,6 +46,17 @@
android:onClick="onStop"
android:text="取消所有请求" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onStopA"
android:text="取消A请求" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onStopB"
android:text="取消B请求" />

<Button
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand All @@ -15,7 +17,20 @@
public abstract class BitmapCallback extends Callback<Bitmap> {
@Override
public Bitmap parseNetworkResponse(Response response, AtomicBoolean isCancelled) throws Exception {
byte[] body = response.getBody();
InputStream is = response.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
byte[] buffer = new byte[2048];
int len = 0;
while ((len = is.read(buffer)) != -1) {
if (isCancelled.get()) {
throw new RuntimeException("http cancel");
}
out.write(buffer, 0, len);
}
out.flush();
is.close();
out.close();
byte[] body = out.toByteArray();
return BitmapFactory.decodeByteArray(body, 0, body.length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ public synchronized int queuedCallsCount() {
return readyAsyncCalls.size();
}

public synchronized void cancelWithTAG(String tag) {
for (RealCall.AsyncCall call : readyAsyncCalls) {
if (tag.equalsIgnoreCase(call.name)) {
call.cancel();
break;
}
}
for (RealCall.AsyncCall call : runningAsyncCalls) {
if (tag.equalsIgnoreCase(call.name)) {
call.cancel();
}
}
}

public synchronized void cancelAll() {
for (RealCall.AsyncCall call : readyAsyncCalls) {
call.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand All @@ -15,7 +17,19 @@ public abstract class JsonCallback extends Callback<JSONObject> {

@Override
public JSONObject parseNetworkResponse(Response response, AtomicBoolean isCancelled) throws Exception {
String results = new String(response.getBody());
return new JSONObject(results);
InputStream is = response.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
byte[] buffer = new byte[2048];
int len = 0;
while ((len = is.read(buffer)) != -1) {
if (isCancelled.get()) {
throw new RuntimeException("http cancel");
}
out.write(buffer, 0, len);
}
out.flush();
is.close();
out.close();
return new JSONObject(new String(out.toByteArray()));
}
}
Loading

0 comments on commit d81a73e

Please sign in to comment.