该应用显示来自网址的文字。我在这个项目中使用了Jsoup,这个应用程序在使用API 23的设备上运行得非常好,但我也想支持KitKat和Lollipop的设备。按钮和TextViews工作但互联网文本不存在。 我希望有人可以帮助我......
这是我的项目的build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.schlauies.supertext"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
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:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'org.jsoup:jsoup:1.10.2'
}

这是XMl文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.schlauies.supertext.MainActivity">
<TextView
android:id="@+id/tx1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="95dp"
android:text="Button" />
</RelativeLayout>
&#13;
这是Java代码:
package com.schlauies.supertext;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
//import orgs.Document;
public class MainActivity extends AppCompatActivity {
TextView texx ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texx =(TextView)findViewById(R.id.tx1);
Button but = (Button) findViewById(R.id.but1);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new doit().execute();
}
});
}
public class doit extends AsyncTask<Void,Void,Void> {
String words = "";
@Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect("http://vertretung.esy.es/gust.txt").get();
words = doc.text();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
texx.setText(words);
}
}
}
&#13;
如果有人有疑问,请询问:)
答案 0 :(得分:0)
您想在清单文件中添加此权限
&lt; uses-permission android:name =&#34; android.permission.INTERNET&#34; /&gt;