2011-04-08 61 views
0
public class ServerResponce extends ListActivity { 
    private ArrayList<listobj> tobj = new ArrayList<listobj>(); 
    **static String str1;** 
    PickUpLocation pickup=new PickUpLocation(); 
    String pickuplocid= pickup.locationid; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    new MyTask().execute(); 

} 

    private class MyTask extends AsyncTask<Void, Void, Void> 
    { 
      private ProgressDialog progressDialog; 
      protected void onPreExecute() { 
        progressDialog = ProgressDialog.show(ServerResponce.this, 
            "", "Loading. Please wait...", true); 
      } 
      @Override 
      protected Void doInBackground(Void... arg0) { 
        try { 





       URL tURL= new URL("http://qrrency.com/mobile/j2me/cab/CabBooking.php?userid=1&from=home&to=office&datetime=&cabservicetype=1&cabtype=1&cabfeatures=AC&locationid="+pickuplocid+""); 

        BufferedReader inn = new BufferedReader(new InputStreamReader(tURL.openStream())); 
        int p=0; 

        StringBuffer buffer1=new StringBuffer(); 

        while ((p=inn.read())!=-1) 
        { 
          buffer1.append((char)p); 
          str1=str1+(char)p; 

        } Log.i("Line----saurabh trivedi-ddddd----", str1); 

        inn.close(); 

的轉移值I使類的對象和str1值傳送到其它類ServerResponce顯示「空」時「STR1」到其他類

servresp=new ServerResponce(); 
String serv= servresp.str1; 
Log.i("tag",serv); 

serv是表示「空」爲什麼?

+0

str1和靜態字符串 – Vivek 2011-04-08 12:48:51

回答

0

當你的字符串是靜態的,那麼你不需要創建該類的Objetc。 可以在第二活動中使用。

ServerResponse.str1; 

在你的第二個活動

+0

ServerResponse.str1;也給「空」 – SRam 2011-04-08 13:10:12

0

ListActivity不說的意思被你實例化一個類,那就是應該由Android本身來處理一個框架的東西。

onCreate()是當一個活動是通過一個意圖開始沒得到,當你實例化類調用,這是Android的工作。所以MyTask不會被創建或執行。

而且,即使你在構造函數中執行MyTask,這將不能工作,因爲:(!這將要執行一段時間)

ServerResponce servresp=new ServerResponce(); 
String serv= servresp.str1; 
Log.i("tag",serv); 

你剛剛起步的任務並立即查詢結果字符串。你必須使用onPostExecute()回調來確保你有一個值。

(你不打算有點太快了?)

相關問題