yanzhenjie / AndServer

:cherries: Web server and web framework of Android platform.
https://yanzhenjie.com/AndServer
Apache License 2.0
3.71k stars 746 forks source link

StorageWebsite使用出现异常 #411

Open jiangshenghui000 opened 2 years ago

jiangshenghui000 commented 2 years ago

1.直接使用示例工程,增加StorageWebsite指定手机的一个目录,http可以访问到静态网页一切正常。
2.自己新建工程,按照文档手动集成,添加StorageWebsite,浏览器访问一直显示ERR_CONNECTION_RESET错误,log也不输出任何错误,无法正常显示页面,Controller接口缺可以正常的访问到。
3.AssetsWebsite的方式正常

下面代码:

####################################################################
@Config
public class AppConfig implements WebConfig {
    private static final String TAG = "AppConfig";
    @Override
    public void onConfig(Context context, Delegate delegate) {
        Log.d(TAG, "onConfig: ");
        delegate.addWebsite(new StorageWebsite( "/sdcard/01987/web2"));
    }
}

####################################################################
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Server server = AndServer.webServer(this)
                .port(8080)
                .timeout(10, TimeUnit.SECONDS)
                .build();
        server.startup();
    }
}

####################################################################
@Controller
public class PageController {
    private static final String TAG = "PageController";

    @GetMapping(path = "/")
    public String index() {
        // Equivalent to [return "/index"].
        Log.d(TAG, "index: ");
        return "forward:/index.html";
    }
}

####################################################################
apply plugin: 'com.android.application'
apply plugin: 'com.yanzhenjie.andserver'

android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.jxkj.webserv"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.yanzhenjie.andserver:api:2.1.9'
    annotationProcessor 'com.yanzhenjie.andserver:processor:2.1.9'
}

####################################################################
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenLocal()
        google { url 'https://maven.aliyun.com/repository/google' }
        jcenter { url 'https://maven.aliyun.com/repository/jcenter' }
        mavenCentral { url 'https://maven.aliyun.com/repository/central' }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath 'com.yanzhenjie.andserver:plugin:2.1.9'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        google { url 'https://maven.aliyun.com/repository/google' }
        jcenter { url 'https://maven.aliyun.com/repository/jcenter' }
        mavenCentral { url 'https://maven.aliyun.com/repository/central' }
    }
[new_file.md](https://github.com/yanzhenjie/AndServer/files/7256105/new_file.md)

}

task clean(type: Delete) {
    delete rootProject.buildDir
}
####################################################################
jiangshenghui000 commented 2 years ago

修改成这个 targetSdkVersion 22 就正常了,同时启动时会弹出一个提示框,"该应用专为旧版android打造……"

jiaosiyuan785 commented 2 years ago

你好,麻烦问下最后您解决了么,我也是改成22就可以,也会遇到同样的提示,但是我还是想改成 version 32,有什么好办法嘛

jiaosiyuan785 commented 2 years ago

你好,我找到解决方式了,其实就是文件权限的问题,我在申请读取和写入后,再AndroidManifest.xml的application中加入了android:requestLegacyExternalStorage="true"这个,那么再targetSdkVersion 32中,也可以正常访问了

jing332 commented 1 year ago

我也有时会遇到 ERR_CONNECTION_RESET, 一番折腾,发现把APP加到电池优化白名单, 再把Server运行在Android前台服务里就行了。