Skip to content

Commit

Permalink
FileCallback,StringCallback,JsonCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin Wu committed Jun 15, 2017
1 parent be053ac commit 89efb9d
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 35 deletions.
15 changes: 9 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.github.why168.androidhttputils"
minSdkVersion 15
targetSdkVersion 25
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -24,7 +24,10 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

compile 'com.android.support.constraint:constraint-layout:1.0.2'

// support
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.why168.androidhttputils.http;

import java.io.IOException;

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

}

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

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

import java.io.IOException;

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

}

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.util.Log;

import com.github.why168.androidhttputils.http.code.HandlerExecutor;
import com.github.why168.androidhttputils.util.StringUtils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -18,6 +19,8 @@
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipInputStream;

import static com.github.why168.androidhttputils.util.StringUtils.getPrintSize;

/**
* @author Edwin.Wu
* @version 2017/6/14 10:06
Expand Down Expand Up @@ -222,7 +225,7 @@ private void getHttpRequest(Request request, GoHttp httpUtils) {

// Log
Log.e("Edwin", "responseCode = " + status + "\n" +
"contentLength = " + getPrintSize(connection.getContentLength()) + "\n" +
"contentLength = " + StringUtils.getPrintSize(connection.getContentLength()) + "\n" +
request.toString());

String encoding = connection.getContentEncoding();
Expand Down Expand Up @@ -315,32 +318,4 @@ String redactedUrl() {
}


public static String getPrintSize(long size) {
//如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
if (size < 1024) {
return String.valueOf(size) + "B";
} else {
size = size / 1024;
}
//如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位
//因为还没有到达要使用另一个单位的时候
//接下去以此类推
if (size < 1024) {
return String.valueOf(size) + "KB";
} else {
size = size / 1024;
}
if (size < 1024) {
//因为如果以MB为单位的话,要保留最后1位小数,
//因此,把此数乘以100之后再取余
size = size * 100;
return String.valueOf((size / 100)) + "."
+ String.valueOf((size % 100)) + "MB";
} else {
//否则如果要以GB为单位的,先除于1024再作同样的处理
size = size * 100 / 1024;
return String.valueOf((size / 100)) + "."
+ String.valueOf((size % 100)) + "GB";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.why168.androidhttputils.http;

import java.io.IOException;

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

}

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.why168.androidhttputils.util;

/**
* @author Edwin.Wu
* @version 2017/6/15 21:48
* @since JDK1.8
*/
public class StringUtils {

public static String getPrintSize(long size) {
//如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
if (size < 1024) {
return String.valueOf(size) + "B";
} else {
size = size / 1024;
}
//如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位
//因为还没有到达要使用另一个单位的时候
//接下去以此类推
if (size < 1024) {
return String.valueOf(size) + "KB";
} else {
size = size / 1024;
}
if (size < 1024) {
//因为如果以MB为单位的话,要保留最后1位小数,
//因此,把此数乘以100之后再取余
size = size * 100;
return String.valueOf((size / 100)) + "."
+ String.valueOf((size % 100)) + "MB";
} else {
//否则如果要以GB为单位的,先除于1024再作同样的处理
size = size * 100 / 1024;
return String.valueOf((size / 100)) + "."
+ String.valueOf((size % 100)) + "GB";
}
}
}
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ allprojects {
task clean(type: Delete) {
delete rootProject.buildDir
}

ext {
// Sdk and tools
minSdkVersion = 15
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = '25.0.3'

// App dependencies
supportLibraryVersion = '25.3.1'

}

0 comments on commit 89efb9d

Please sign in to comment.