2017-11-11 16 views
0

私は自分のサーバに行くアクティビティを持っており、テキストファイルを取得します。このテキストファイルは、パッケージ名を含む1行のテキストを保持します。私の目標は、パッケージ名を取得し、パッケージ名を使用して、サーバー上の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

新しいgetter.BackTask()。execute( "http://1.2.3.4/test.txt")) 'が2回あります。どうして?いいよねさらに、AsyncTaskから結果を得ることはできません。 onPostExecuteでdoInBackgroundの結果を処理する必要があります。ただそこに! – greenapps

答えて

0

のという変数は、その方法のローカルです。そのメソッドが正しく呼び出されたとしても、他のメソッドはその値を認識しません。

あなたは、変数を宣言text場所の近く、BackTaskの上部付近packageNameを宣言しようとすることができます。

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

免責事項:私はあなたのコードまたはこの修正プログラムをロードして実行しようとしていない

その後にこの方法を変更!

+0

私が使用して正しくに呼び出しています:。mContext.getPackageManager()getPackageInfo(。String.valueOf(新getter.BackTask())( "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間で通信を行うために、問題が発生していると思いますの戻り値は

onPostExecuteにAsynctaskを

の変化を、それを渡すためにをdoInBackground

//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")). 

を持っているのはなぜ?いいよね

さらに、あなたはあなたがonPostExecuteでdoInBackgroundの結果を処理する必要があります

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

でAsyncTaskからの結果を得ることができません。ただそこに!

関連する問題