因此,我有一个活动让我的服务器读取一个文本文件。该文本文件包含一行包含包名称的文本。我的目标是获取软件包名称,然后使用软件包名称获取服务器上txt文件中指定软件包的versionCode。 这里是获取来自服务器的txt文件类:如何从服务器上的txt文件返回软件包名称
public class getter extends Activity {
Activity context;
TextView txtview;
ProgressDialog pd;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get);
context=this;
}
public void onStart(){
super.onStart();
BackTask bt=new BackTask();
bt.execute("http://1.2.3.4/test.txt");
}
//background process to download the file from internet
public static class BackTask extends AsyncTask<String,Integer,Void>{
String text="";
protected void onPreExecute(){
super.onPreExecute();
//display progress dialog
}
protected Void doInBackground(String...params){
URL url;
try {
//create url object to point to the file location on internet
url = new URL(params[0]);
//make a request to server
HttpURLConnection con=(HttpURLConnection)url.openConnection();
//get InputStream instance
InputStream is=con.getInputStream();
//create BufferedReader object
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line;
//read content of the file line by line
while((line=br.readLine())!=null){
text+=line;
}
br.close();
}catch (Exception e) {
e.printStackTrace();
//close dialog if error occurs
}
return null;
}
protected void onPostExecute(Void result){
String packageName = text;
}
}
public String getPackageName(Context mContext) {
if (mContext != null) {
BackTask bt=new BackTask();
bt.execute("http://1.2.3.4/test.txt");
}
return "";
}
}
,这是应该从服务器上指定的包拿到的versionCode:
public static int getinstVersionCode(Context mContext) {
if (mContext != null) {
try {
getter.BackTask bt=new getter.BackTask();
bt.execute("http://1.2.3.4/test.txt");
return mContext.getPackageManager().getPackageInfo(String.valueOf(new getter.BackTask().execute("http://1.2.3.4/test.txt")), 0).versionCode;
} catch (PackageManager.NameNotFoundException ignored) {
}
}
return 0;
}
为什么没有这个回报服务器上软件包名称的versionCode?
我认为错误在于下面的函数,但我不确定。
返回mContext.getPackageManager()getPackageInfo(将String.valueOf(新getter.BackTask()执行( “http://1.2.3.4/test.txt”)),0).versionCode。
你有什么异常吗?你能否详细描述一下发生的事情。另外,getter.BackTask()。execute返回null,它应该返回从服务器获得的文本。它应该返回一个字符串,并且不会像你指定的那样无效 – user3362334
你有两次'new getter.BackTask()。execute(“http://1.2.3.4/test.txt”))''。为什么?看起来不太好。此外,您无法以这种方式从AsyncTask获得结果。你应该在onPostExecute中处理doInBackground的结果。只有在那里! – greenapps