Commit a226d4c2 by LAPTOP-146U5DF5\28422

修改了请求APK资源的逻辑:(1)从直接请求APK数据->请求APK数据的下载url,进行下载…

修改了请求APK资源的逻辑:(1)从直接请求APK数据->请求APK数据的下载url,进行下载 (2)原来是依靠请求之后,收到的头Content-Disposition中获取APK的包名->现在是直接从url中获取
parent 10384643
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<codeStyleSettings language="XML">
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>ANDROID_ATTRIBUTE_ORDER</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
......@@ -35,4 +35,5 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
}
\ No newline at end of file
......@@ -14,6 +14,12 @@ import java.util.concurrent.Executors;
import com.fear1ess.reyunaditool.MainActivity.MainActivityHandler;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static com.fear1ess.reyunaditool.NetWorkUtils.getDwonURL;
public class DoWorkThread implements Runnable {
public Handler mainUiHandler = null;
public Context appContext = null;
......@@ -27,6 +33,18 @@ public class DoWorkThread implements Runnable {
@Override
public void run() {
// MyLog.d("开始获取下载连接: ");
// String url=getDwonURL();
// MyLog.d("获取到的下载地址: "+url);
// if(url==null||url.length()<10){
// Message message = Message.obtain();
// message.what = MainActivityHandler.NETWORK_ERROR;
// Bundle bundle = new Bundle();
// bundle.putCharSequence("errText","获取app下载连接!请检查网络连接");
// message.setData(bundle);
// mainUiHandler.sendMessage(message);
// return;
// }
NetWorkUtils.Response res = NetWorkUtils.get(getAppInfoUrlStr,null);
if(res.resCode != 200){
......@@ -38,6 +56,7 @@ public class DoWorkThread implements Runnable {
mainUiHandler.sendMessage(message);
return;
}
MyLog.d("开始下载:");
try {
JSONArray appInfoArray = new JSONObject(new String(res.resData)).getJSONArray("res");
ExecutorService es = Executors.newSingleThreadExecutor();
......
......@@ -19,10 +19,18 @@ import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static com.fear1ess.reyunaditool.DoWorkThread.getAppInfoUrlStr;
import static com.fear1ess.reyunaditool.ExecuteCmdUtils.executeCmd;
public class MainActivity extends AppCompatActivity {
......@@ -70,6 +78,7 @@ public class MainActivity extends AppCompatActivity {
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startBtn.setText("Working");
startBtn.setEnabled(false);
stopBtn.setEnabled(true);
......@@ -130,6 +139,29 @@ public class MainActivity extends AppCompatActivity {
}
public void onClick2(View view) {
new Thread(new Runnable() {
@Override
public void run() {
JSONObject jsonObject=null;
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("http://adfly-api.adinsights-global.com/get_app_res/?count=1&ua=android")
.method("GET", null)
.build();
try {
Response response = client.newCall(request).execute();
String result= new String(response.body().bytes());
jsonObject=new JSONObject(result);
MyLog.d(jsonObject.toString());
} catch (Exception e) {
MyLog.error(""+e.toString());
}
}
}).start();
}
public static class MainActivityHandler extends Handler{
public static final int NETWORK_ERROR = 0;
public static final int StartMySlef = 11;
......@@ -175,6 +207,7 @@ public class MainActivity extends AppCompatActivity {
}
public static void startMySelf(){
try{
if(MainActivity.m_context==null)return;
int result= ExecuteCmdUtils.startApp(MainActivity.m_context, MainActivity.m_myPackageName);
if(result ==-1){
MyLog.error("启动Activity结果失败");
......
......@@ -16,6 +16,7 @@ public class MyLog {
public static void error(Object object){
synchronized (object){
Log.d(TAG, "异常:"+object);
Log.d(TAG, Log.getStackTraceString(new Throwable()));
}
}
}
......@@ -3,6 +3,8 @@ package com.fear1ess.reyunaditool;
import android.content.res.Resources;
import android.util.Log;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
......@@ -32,6 +34,12 @@ import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static com.fear1ess.reyunaditool.DoWorkThread.getAppInfoUrlStr;
public class NetWorkUtils {
public interface NetWorkCallback {
......@@ -51,6 +59,7 @@ public class NetWorkUtils {
os = new ByteArrayOutputStream();
}
Response res = null;
MyLog.d("get =========== "+urlStr);
if(urlStr.startsWith("https://")) res = httpsReq(urlStr,headers,null,os);
else if(urlStr.startsWith("http://")) res = httpReq(urlStr,headers,null,os);
try {
......@@ -183,6 +192,7 @@ public class NetWorkUtils {
private static Response httpsReq(String urlStr, HashMap<String,String> headers,byte[] postData,OutputStream os){
URL getAppInfoUrl = null;
Response res = null;
MyLog.d("httpsReq ====== "+urlStr);
try {
getAppInfoUrl = new URL(urlStr);
HttpsURLConnection huc = (HttpsURLConnection) getAppInfoUrl.openConnection();
......@@ -216,7 +226,8 @@ public class NetWorkUtils {
if(os instanceof ByteArrayOutputStream) {
resData = ((ByteArrayOutputStream)os).toByteArray();
}
String fileName = huc.getHeaderField("Content-Disposition");
String fileName = urlStr.split("https://adfly-facebook-1258892624.cos.ap-hongkong.myqcloud.com/apks/")[1].split(".apk")[0];
MyLog.d("获取的文件名:"+fileName);
res = new Response(huc.getResponseCode(),huc.getResponseMessage(),resData,fileName);
os.close();
is.close();
......@@ -237,6 +248,28 @@ public class NetWorkUtils {
return res;
}
//获取下载地址
public static String getDwonURL(){
String result=null;
JSONObject jsonObject=null;
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url(getAppInfoUrlStr)
.method("GET", null)
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
String resp= new String(response.body().bytes());
jsonObject=new JSONObject(resp);
MyLog.d(jsonObject.toString());
result=jsonObject.optString("download_url","");
} catch (Exception e) {
MyLog.error(""+e.toString());
}
return result;
}
public static class Response{
public int resCode;
......
......@@ -8,6 +8,7 @@ import android.util.Log;
import com.fear1ess.reyunaditool.AppInfo;
import com.fear1ess.reyunaditool.DoCommandService;
import com.fear1ess.reyunaditool.ExecuteCmdUtils;
import com.fear1ess.reyunaditool.MyLog;
import com.fear1ess.reyunaditool.NetWorkUtils;
import com.fear1ess.reyunaditool.state.AppState;
import com.fear1ess.reyunaditool.utils.PushMsgUtils;
......@@ -21,6 +22,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import static com.fear1ess.reyunaditool.NetWorkUtils.getDwonURL;
public class DownloadThread extends Thread {
private Handler mainUiHandler;
......@@ -52,6 +55,7 @@ public class DownloadThread extends Thread {
public String downloadApp(String downloadUrl, String downloadPath) {
Log.d(TAG, "downloadApp...");
NetWorkUtils.Response res = NetWorkUtils.download(downloadUrl,null,null,downloadPath);
MyLog.d("downloadApp 结果:"+res);
if(res == null) return null;
return res.resFileName;
}
......@@ -92,18 +96,22 @@ public class DownloadThread extends Thread {
public void downloadAppLoop(){
while(needRunning){
MyLog.d("downloadAppLoop");
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
MyLog.d("开始获取下载连接: ");
String url=getDwonURL();
MyLog.d("获取到的下载地址: "+url);
setNetWorkCallback();
String ts = String.valueOf(System.currentTimeMillis());
String downloadPath = downloadDir + "/" + "rycache_" + ts + ".apk";
String pkgName = downloadApp(downloadAppUrlStr, downloadPath);
String pkgName = downloadApp(url, downloadPath);
// String pkgName = downloadApp(downloadAppUrlStr, downloadPath);
MyLog.d("下载获取的包名:"+pkgName);
if(pkgName == null) {
ExecuteCmdUtils.deletePkg(downloadPath);
try {
......
......@@ -27,4 +27,11 @@
android:layout_height="wrap_content">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick2"
>
</Button>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment