我有一个活动进入我的服务器并获取一个带有字符串的.txt文件。我想从另一个活动中调用.txt文件中的字符串,以用作函数中的变量。我怎样才能公开这个?我如何从其他活动中调用它? 代码:
public class getter extends AppCompatActivity {
String TextFileURL = "http://1.2.3.4/test.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_get);
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);
}
}
我试图用这个来调用字符串:
public static int getinstVersionCode(Context mContext) {
if (mContext != null) {
try {
return mContext.getPackageManager().getPackageInfo(getter.GetNotePadFileFromServer., 0).versionCode;
} catch (PackageManager.NameNotFoundException ignored) {
}
}
return 0;
}