2010-06-09 2 views
0

j'utilise ci-dessous webservices pour récupérer des données d'un côté serveur serveur : .net côté client: ksoap2écran noir lors de la récupération résultat de webservices dans Android

whenever activity start, onCreate i am using spinner for displying data returned by the webservices 
when this activity start it showing black screen after lunching the activity .i found black screen is coming when activity connecting to webservices 

How to resolve this 


MyCode 

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
try { 
       //Display the online and busy people display in spinner 
       //people are display in relative people only(Mygroup) 
       /* get the online and busy people who are in user group from DB*/ 
       users_names_ids=new ParseXMLString().convertusernames(new DataParsingComm().ILGetOnlinePeoples("<spGetOnlinePeoples><UserID>"+GetCurrentUserID.id+"</UserID></spGetOnlinePeoples>")); 

       /* create an array with the size of number of peoples whose status is online or busy */ 
       String[] array =new String[users_names_ids.size()];   
       int setselction=0;// initialize the selection to 0. 

       /* if array length is greater than zero, that means getting at least one person whose status is online or busy */ 
       if(array.length>0){ 

        /* Returns an enumeration on the keys of this Hashtable instance. And assigns into Enumeration instance variable */ 
        Enumeration e= users_names_ids.keys(); 

        /* Iterate list Enumeration until it does't has any more elements */ 
        for(int i=0;e.hasMoreElements();i++) 
         try{ 
          /* get all persons names into the array list */ 
          array[i]=e.nextElement().toString(); 

          /* Get the ChatUserName value from the ChatInPeopleDetails preferences. And If it is in this list set selection to the index 'i' */ 
          if(getSharedPreferences("ChatInPeopleDetails", 0).getString("ChatUserName", "").equals(array[i])) 
           setselction=i; 

           /* 
           * Get the String value of Relname, that previously added with putExtra() as extended data to the parent intent 
           * If that value is not null and exists in the array list then 
           * set the selection to the index 'i'. 
           * */ 
          else if(getIntent().getStringExtra("Relname")!=null && getIntent().getStringExtra("Relname").equals(array[i])) 
             setselction=i; 

         }catch(Exception ex){ 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          System.gc(); 
          System.runFinalization(); 
         } 
       } 

       /* create a new array adapter with the ChatForm context and array objects */ 
       ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(ChatForm.this,android.R.layout.simple_spinner_item, array); 

       /* Set the layout resource to create the drop down views. */ 
       adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

       /* The Adapter is used to provide the data which backs this Spinner SpinnerUsersToChat. */ 
       ((Spinner)findViewById(R.id.SpinnerUsersToChat)).setAdapter(adapter2); 

       /* Get the ChatUserName value from the ChatInPeopleDetails preferences. If this value is not null*/ 
       if(getSharedPreferences("ChatInPeopleDetails", 0).getString("ChatUserName", "") !=null) 
       { 
        /* Set the currently selected item of spinner based on selection variable value */ 
        ((Spinner)findViewById(R.id.SpinnerUsersToChat)).setSelection(setselction); 
       } 

       /* Register a callback to be invoked when an item in this AdapterView has been selected.*/ 
       ((Spinner)findViewById(R.id.SpinnerUsersToChat)).setOnItemSelectedListener(new OnItemSelectedListener() 
       { 
        public void onItemSelected(AdapterView<?> parent,View v,int position,long id) 
         {     
          /* call getMsg() to get messages and display them*/ 
          getMsg(); 
          /* Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.*/ 
          ((ScrollView)findViewById(R.id.ScrollView06)).post(new Runnable() 
           { 
           public void run() 
           { 
            /* This fullScroll() method will scroll the view to the bottom .*/ 
            ((ScrollView)findViewById(R.id.ScrollView06)).fullScroll(View.FOCUS_DOWN); 
           } 

          }); 
         } 
         /* on nothing selected to do somthing . this an overridden method */ 
         public void onNothingSelected(AdapterView<?> arg0) { 

         } 

       }); 

      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 

}

+0

pouvez-vous s'il vous plaît poster votre code –

+0

Faites-vous cet appel de service Web sur le fil de l'interface utilisateur? –

+0

Salut Mina et Rpond .i posté le code et partager votre idée – Aswan

Répondre

1

On dirait que vous essayez faire votre demande sur le thread principal de l'interface utilisateur. Parce que la demande Web prend du temps, le thread d'interface utilisateur doit attendre jusqu'à ce qu'il soit terminé avant de pouvoir mettre à jour l'écran.

Vous devez utiliser un AsyncTask faire cette demande en arrière-plan:

class WebRequestTask extends AsyncTask<String, int[], String[]> { 

@Override 
protected String[] doInBackground(String... params) { 
    String[] results = null; 

    // Anything done here is in a seperate thread to the UI thread 
    // Do you download from here 

    // If you want to update the progress you can call 
    publishProgress(int progress); // This passes to the onProgressUpdate method 

    return results; // This passes the result to the onPostExecute method 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    // This is on your UI thread, useful if you have a progressbar in your view 
} 

@Override 
protected void onPostExecute(String[] results) { 
    super.onPostExecute(results); 
    // This is back on your UI thread - 
    } 
} 

Démarrez votre AsyncTask avec

new WebRequestTask().execute(); 

Jetez un oeil à http://developer.android.com/reference/android/os/AsyncTask.html pour plus d'info

+0

Thankq pour votre réponse je vais essayer maintenant – Aswan

Questions connexes