-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,134 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.cczhr.otglocation.net | ||
|
||
import okhttp3.ResponseBody | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Headers | ||
import retrofit2.http.Streaming | ||
import retrofit2.http.Url | ||
|
||
|
||
/** | ||
* @author cczhr | ||
* @description | ||
* @since 2021/6/10 10:06 | ||
*/ | ||
interface Api { | ||
@GET("repos/iGhibli/iOS-DeviceSupport/contents/DeviceSupport") | ||
suspend fun getDeviceSupport(): DeviceSupportBean? | ||
|
||
@Streaming | ||
@GET | ||
fun get(@Url fileUrl: String): Call<ResponseBody> | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/cczhr/otglocation/net/DeviceSupportBean.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.cczhr.otglocation.net | ||
|
||
/** | ||
* @author cczhr | ||
* @description | ||
* @since 2021/6/10 09:33 | ||
*/ | ||
class DeviceSupportBean : ArrayList<DeviceSupportBeanItem>() | ||
|
||
data class DeviceSupportBeanItem( | ||
val _links: Links, | ||
val download_url: String, | ||
val html_url: String, | ||
val name: String, | ||
val path: String, | ||
val sha: String, | ||
val size: Any, | ||
val type: String, | ||
val url: String | ||
) | ||
|
||
data class Links( | ||
val html: String, | ||
val self: String | ||
) |
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/cczhr/otglocation/net/LoggingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.cczhr.otglocation.net | ||
|
||
import android.util.Log | ||
import okhttp3.* | ||
import okio.Buffer | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
|
||
/** | ||
* @author cczhr | ||
* @description | ||
* @since 2021/6/10 10:17 | ||
*/ | ||
class LoggingInterceptor : Interceptor { | ||
val TAG = "LoggingInterceptor" | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request: Request = chain.request() | ||
val t1 = System.nanoTime() //请求发起的时间 | ||
|
||
val method: String = request.method() | ||
val jsonObject = JSONObject() | ||
if ("POST" == method || "PUT" == method) { | ||
if (request.body() is FormBody) { | ||
val body = request.body() as? FormBody | ||
if (body != null) { | ||
for (i in 0 until body.size()) { | ||
try { | ||
jsonObject.put(body.name(i), body.encodedValue(i)) | ||
} catch (e: JSONException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
Log.e( | ||
TAG, | ||
"request" + java.lang.String.format( | ||
"发送请求 %s on %s %nRequestParams:%s%nMethod:%s", | ||
request.url(), chain.connection(), jsonObject.toString(), request.method() | ||
) | ||
) | ||
} else { | ||
val buffer = Buffer() | ||
val requestBody: RequestBody? = request.body() | ||
if (requestBody != null) { | ||
request.body()!!.writeTo(buffer) | ||
val body: String = buffer.readUtf8() | ||
Log.e( | ||
TAG, | ||
"request" + java.lang.String.format( | ||
"发送请求 %s on %s %nRequestParams:%s%nMethod:%s", | ||
request.url(), chain.connection(), body, request.method() | ||
) | ||
) | ||
} | ||
} | ||
} else { | ||
Log.e( | ||
TAG, | ||
"request" + java.lang.String.format( | ||
"发送请求 %s on %s%nMethod:%s", | ||
request.url(), chain.connection(), request.method() | ||
) | ||
) | ||
} | ||
val response = chain.proceed(request) | ||
return try { | ||
val t2 = System.nanoTime() //收到响应的时间 | ||
val responseBody = response.peekBody((1024 * 1024).toLong()) | ||
Log.e( | ||
TAG, | ||
"request" + String.format( | ||
"接收响应: %s %n返回json:【%s】 %n耗时:%.1fms", | ||
response.request().url(), | ||
responseBody.string(), | ||
(t2 - t1) / 1e6 | ||
) | ||
) | ||
response | ||
} catch (e: Exception) { | ||
response | ||
} | ||
} | ||
} |
Oops, something went wrong.