我正在尝试使用存储在服务器上的5个单独的.txt文件的内容填充5个单独的edittext框(只需单击一下按钮)。 我可以使用下面的代码填充第一个edittext
package fromserver.lou.readfromweb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
String TextFileURL = "http://saturn.glyndwr.ac.uk/n3rds/Receipt.txt" ;
TextView textView ;
Button button ;
URL url ;
String TextHolder = "" , TextHolder2 = "";
BufferedReader bufferReader ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new GetNotePadFileFromServer().execute();
}
});
}
public class GetNotePadFileFromServer extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
url = new URL(TextFileURL);
bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((TextHolder2 = bufferReader.readLine()) != null) {
TextHolder += TextHolder2;
}
bufferReader.close();
} catch (MalformedURLException malformedURLException) {
// TODO Auto-generated catch block
malformedURLException.printStackTrace();
TextHolder = malformedURLException.toString();
} catch (IOException iOException) {
// TODO Auto-generated catch block
iOException.printStackTrace();
TextHolder = iOException.toString();
}
return null;
}
@Override
protected void onPostExecute(Void finalTextHolder) {
textView.setText(TextHolder);
super.onPostExecute(finalTextHolder);
}
}
}
我想也许,如果我能以某种方式从主要活动中取出代码并拥有一个特殊的类,并为每个edittext执行五次它可能有效,但我无法做到。我试着将课程分开如下:
package fromserver.lou.readfromweb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
Button button ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new GetNotePadFileFromServer().execute();
}
});
}
}
并创建了一个新课程:
package fromserver.lou.readfromweb;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by lou on 27/04/2017.
*/
public class GetNotePadFileFromServer extends AsyncTask<Void, Void, Void> {
URL url ;
String TextHolder = "" , TextHolder2 = "";
BufferedReader bufferReader ;
String TextFileURL = "http://saturn.glyndwr.ac.uk/n3rds/Receipt.txt" ;
TextView textView ;
@Override
protected Void doInBackground(Void... params) {
try {
url = new URL(TextFileURL);
bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((TextHolder2 = bufferReader.readLine()) != null) {
TextHolder += TextHolder2;
}
bufferReader.close();
} catch (MalformedURLException malformedURLException) {
// TODO Auto-generated catch block
malformedURLException.printStackTrace();
TextHolder = malformedURLException.toString();
} catch (IOException iOException) {
// TODO Auto-generated catch block
iOException.printStackTrace();
TextHolder = iOException.toString();
}
return null;
}
@Override
protected void onPostExecute(Void finalTextHolder) {
textView.setText(TextHolder);
super.onPostExecute(finalTextHolder);
}
}
但是在主要活动中,textView保持红色,并且表示无法解析符号textview。
我打算这样做5次,只是稍微更改每个类,指向不同的URL和单独的edittext框,我还假设我必须在主活动中点击监听器添加一些内容来调用新的类。