Skip to content

Commit

Permalink
1.修复 下拉列表无限增加重复连接
Browse files Browse the repository at this point in the history
2.修复 http 连接问题,增加 异常输出区
3.打开连接,记住上次使用的连接
  • Loading branch information
1174657425436453 committed Sep 23, 2022
1 parent f2ce343 commit 043de57
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 15 deletions.
20 changes: 20 additions & 0 deletions app/debug/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.baiyu.cewangsu",
"variantName": "debug",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0",
"outputFile": "app-debug.apk"
}
],
"elementType": "File"
}
Binary file renamed app/debug/cewangsu.apk → app/debug/网速Beta.apk
Binary file not shown.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
android:roundIcon="@drawable/ic_launcher1"
android:supportsRtl="true"
android:theme="@style/Theme.Cewangsu"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="31" >
<!--networkSecurityConfig 网络上面说高版本安卓 http 支持噢-->
<activity
android:name=".MainActivity"
android:exported="true" >
Expand Down
55 changes: 53 additions & 2 deletions app/src/main/java/com/baiyu/cewangsu/ApplicationFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void createConfigurationFile(Activity act){

if(jsonString.length() == 0){
System.out.println("文件空内容:写入成功");
jsonString="{\"url\":[\"https:\\/\\/baidu.com\",\"https:\\/\\/pm.myapp.com\\/invc\\/xfspeed\\/qqpcmgr\\/download\\/QQPCDownload320001.exe\"],\"TCP\":true,\"UP\":false,\"switch\":true}";
jsonString="{\"url\":[\"https:\\/\\/baidu.com\",\"https:\\/\\/pm.myapp.com\\/invc\\/xfspeed\\/qqpcmgr\\/download\\/QQPCDownload320001.exe\"],\"TCP\":true,\"UP\":false,\"switch\":true,\"pos\":1}";

}
else {
Expand All @@ -67,14 +67,23 @@ public static void createConfigurationFile(Activity act){
}

public static void setJsonObjectUrl(String url) {
boolean bool=isExisitInArray(url);//判断是否存在再加,不然会重复 false 为不可加 true 为可添加
try {


jsonArray=jsonObject.getJSONArray("url");
// System.out.println("测试"+jsonArray);
} catch (JSONException e) {
throw new RuntimeException(e);
}

jsonArray.put(url);
if (bool) {//true 表示要添加
jsonArray.put(url);
}
else {//有就啥都不做
return;
}


try {
jsonObject.put("url",jsonArray);
Expand Down Expand Up @@ -163,4 +172,46 @@ public static void getArraySpinnerFromFile(List<String> spinnerData){
throw new RuntimeException(e);
}
}


public static boolean isExisitInArray(String url){//true 表示 连接不存在 false 表示 连接存在
JSONArray jsonArray1= null;
//url=url.replace("//","\\/\\/");

try {
jsonArray1 = jsonObject.getJSONArray("url");
} catch (JSONException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < jsonArray1.length(); i++) {
try {
if (url.equals(jsonArray1.getString(i))) {
return false;
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

return true;
}

public static void insertUrlRecord(int position){//记录下列列表的位置
try {

jsonObject.remove("pos");
jsonObject.put("pos",position);//
loadFile();
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

public static int getUrlRecord(){
try {
return jsonObject.getInt("pos");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/baiyu/cewangsu/DownloadServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import android.widget.Toast;
import androidx.annotation.Nullable;

public class DownloadServer extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
Expand All @@ -29,6 +31,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if(TCP) {
if(!UP){
downloadThread = new DownloadThread();//TCP 下载

ApplicationFile.setJsonObjectUrl(intent.getStringExtra("url"));
}
else {
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/com/baiyu/cewangsu/DownloadThread.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.baiyu.cewangsu;

import android.net.http.HttpResponseCache;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import java.io.IOException;
Expand All @@ -15,6 +17,8 @@ public class DownloadThread implements Runnable{
static boolean isRun=false;//控制线程停止 true 为运行
static String url;//下载链接
static String path;//文件路径
static Handler loghandler;//异常handler


@Override
public void run() {
Expand Down Expand Up @@ -50,13 +54,21 @@ public void run() {
}

} catch (MalformedURLException e) {
throw new RuntimeException(e);
Message msg = new Message();
msg.obj=e.toString();
loghandler.sendMessage(msg);
}
catch (UnknownHostException e){
e.printStackTrace();
Message msg = new Message();
msg.obj=e.toString();
loghandler.sendMessage(msg);
}
catch (IOException e) {
throw new RuntimeException(e);

Message msg = new Message();
msg.obj=e.toString();
loghandler.sendMessage(msg);

}

}
Expand Down
61 changes: 53 additions & 8 deletions app/src/main/java/com/baiyu/cewangsu/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
import java.util.ArrayList;
import java.util.List;


/**
* 欠流量统计功能
* http 服务器响应数据异常 更新ui 未解决 ✔
* 下拉列表无删除功能
* 下拉列表 无限增加连接 ✔
* app 再次打开 无法恢复上次使用的url ✔
* 实时网速有问题
* 后台运行按钮需要调整
* 重要一点 app 需要重构 代码沉余验证
* 作者对于权限 也是迷迷糊糊 不知道要不要申请
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener,RadioGroup.OnCheckedChangeListener,AdapterView.OnItemSelectedListener {
//下载链接
EditText downloadUrl;
Expand Down Expand Up @@ -47,18 +57,25 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
RadioButton radioButtonTCP;//TCP协议选择

RadioButton radioButtonUDP;//UDP 协议选择
Handler wsHnadler ;
Handler wsHnadler ;//网速handler
Handler logHandler;

Spinner spinnerURL;//连接下拉列表
Spinner spinnerURL;//连接下拉列表

List<String> spinnerDate;

ArrayAdapter<String> arrayAdapter;

static TextView logError;

/**下拉列表第一次打开 自动就执行了监听方法 气死了 试了其他方法都不行, 只能这样了*/
static boolean isFirst=true;//true 表示第一次打开app
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

logError=findViewById(R.id.logArea);
downloadUrl=findViewById(R.id.download_url);
startButton= findViewById(R.id.start_button);
startButton.setOnClickListener(this);//开始按钮
Expand Down Expand Up @@ -120,7 +137,9 @@ protected void onCreate(Bundle savedInstanceState) {
ApplicationFile.getArraySpinnerFromFile(spinnerDate);//获取json 对象数组连接 添加到列表
arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,spinnerDate);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

spinnerURL.setAdapter(arrayAdapter);//下拉列表添加适配器

//为下拉列表设置监听器
spinnerURL.setOnItemSelectedListener(this);
}
Expand Down Expand Up @@ -189,10 +208,22 @@ private void downloadDisribution(){
boolean UP=radioButtonTCP.isChecked();
DownloadThread.url =url;//下载链接
DownloadThread.path=path;//文件路径

//异常更新ui
this.logHandler=new Handler() {
@Override
public void handleMessage(Message msg){
logError.setText(msg.obj.toString());
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
};

if(TCP) {
if(UP){
// downloadThread = new DownloadThread();//TCP 下载
DownloadThread.isRun=true;//为true 要求线程运行
DownloadThread.loghandler=this.logHandler;//让子线程能通知主线程修改ui
ApplicationFile.setSwitchCondiction(true,false);
}
else {
Expand All @@ -215,7 +246,9 @@ private void downloadDisribution(){
Toast.makeText(this, "还需要在输入框输入ip和端口呢!!!",Toast.LENGTH_SHORT).show();
return;
}
System.out.println("zhixingdaol");


DownloadThread.loghandler=this.logHandler;
ApplicationFile.removeSwitchCondiction();
ApplicationFile.setSwitchCondiction(false,true);

Expand All @@ -225,7 +258,9 @@ private void downloadDisribution(){
return;
}
}

if (ApplicationFile.isExisitInArray(downloadUrl.getText().toString())) {//true 表示连接不存在 要加连接?
ApplicationFile.insertUrlRecord(spinnerDate.size());//下拉列表长度加1
}
startService(intent);

startButton.setEnabled(false);
Expand Down Expand Up @@ -293,12 +328,22 @@ protected void onActivityResult(int requestCode,int resultCode,Intent data) {

@Override//下拉列表的监听
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
downloadUrl.setText(arrayAdapter.getItem(i));
System.out.println("wuwuwuwuwuwu");
if(isFirst){
downloadUrl.setText(arrayAdapter.getItem(ApplicationFile.getUrlRecord()));
isFirst=false;
}//第一次 在文件读取 位置
else {


//其他靠点击 下拉列表获取位置
ApplicationFile.insertUrlRecord(i);
downloadUrl.setText(arrayAdapter.getItem(i));
}
}

@Override//下拉列表的监听
public void onNothingSelected(AdapterView<?> adapterView) {
downloadUrl.setText("hello");
public void onNothingSelected(AdapterView<?> adapterView) {//Adapter 为空时候执行这个方法

}
}
22 changes: 20 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@
android:layout_width="800px" android:layout_height="80px"/>

</LinearLayout>

<TextView
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:id="@+id/totalUsage"
android:layout_width="150dp" android:layout_height="wrap_content"
android:text="历史总流量:"
/>
<TextView
android:id="@+id/wang_su"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="网速:"
/>
</LinearLayout>
<Button android:id="@+id/start_button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="开始"
Expand All @@ -96,4 +102,16 @@

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="仅供学习交流,严禁用于商业用途,请于24小时内删除。严禁利用软件损害他人合法利益,对使用本软件造成的任何损失,本作者概不负责"/>

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="ErrorLog(输出区):"
android:textColor="@color/red_001"/>
<TextView
android:id="@+id/logArea"
android:layout_width="wrap_content" android:layout_height="wrap_content"

/>
</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red_001">#FF0000</color>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true"/>
</network-security-config>

0 comments on commit 043de57

Please sign in to comment.