2017-11-11 180 views
0

因此,我有一个活动让我的服务器读取一个文本文件。该文本文件包含一行包含包名称的文本。我的目标是获取软件包名称,然后使用软件包名称获取服务器上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。

+0

你有什么异常吗?你能否详细描述一下发生的事情。另外,getter.BackTask()。execute返回null,它应该返回从服务器获得的文本。它应该返回一个字符串,并且不会像你指定的那样无效 – user3362334

+0

你有两次'new getter.BackTask()。execute(“http://1.2.3.4/test.txt”))''。为什么?看起来不太好。此外,您无法以这种方式从AsyncTask获得结果。你应该在onPostExecute中处理doInBackground的结果。只有在那里! – greenapps

回答

0

一个问题是,您的变量packageNameonPostExecute是该方法的本地。所以即使该方法被正确调用,其他方法也不会看到该值。

您可以尝试声明packageNameBackTask的顶部附近,在您声明变量text附近。

则此方法更改为:

protected void onPostExecute(Void result){ 
    packageName = text; 
} 

免责声明:我没有尝试加载和运行代码或此修复程序!

+0

我是否正确地调用它:return mContext.getPackageManager()。getPackageInfo(String.valueOf(new getter.BackTask()。execute(“http://1.2.3.4/test.txt”)),0)。的versionCode; – Zuuchq

+0

我看不出它是否完全正确。为了调试的目的,你可以把这一行分成几行: 'return mContext.getPackageManager()。getPackageInfo(String.valueOf(new getter.BackTask()。execute(“http://1.2.3.4/test.txt” )),0).versionCode;' 而不是仅仅返回它。然后你可以在'return'之前记录'execute'部分的输出。你现在有什么让你很难看到你是否正在阅读包名。 – akubot

+0

另外,您是否看到Bobby Prabowo的建议?你可以尝试一下,并解决我之前提到的局部变量问题。 – akubot

0

如果网络没有任何问题,那么想,因为你使用的的AsyncTask =>文本

应使用内部变量doInBackgroundonPostExecute之间沟通的问题引起的在返回值doInBackground到它的AsyncTask传递给onPostExecute

变化

//background process to download the file from internet 
public static class BackTask extends AsyncTask<String,Integer,String>{ 
    protected void onPreExecute(){ 
    super.onPreExecute(); 
    //display progress dialog 

} 



protected String doInBackground(String...params){ 
    URL url; 
    String text; 
    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 text; 

} 


protected void onPostExecute(String resultText){ 
    String packageName = resultText; 


} 


} 
0

你有两次

new getter.BackTask().execute("http://1.2.3.4/test.txt")). 

为什么?看起来不太好。

而且你不能从一个的AsyncTask得到的结果与

String.valueOf(new getter.BackTask().execute("http://1.2.3.4/test.txt")) 

您应该处理doInBackground的结果onPostExecute。只有在那里!

相关问题