私は自分のサーバに行くアクティビティを持っており、テキストファイルを取得します。このテキストファイルは、パッケージ名を含む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。
例外がありますか?何が起きているのか詳しく説明してください。また、getter.BackTask()。executeは、サーバーから取得したテキストを返す必要があるnullを返します。これは文字列を返すべきであり、あなたが指定した通りに無効になることはありません。 – user3362334
新しいgetter.BackTask()。execute( "http://1.2.3.4/test.txt")) 'が2回あります。どうして?いいよねさらに、AsyncTaskから結果を得ることはできません。 onPostExecuteでdoInBackgroundの結果を処理する必要があります。ただそこに! – greenapps