我正在使用以下Web服务从服务器服务器端检索数据:.net客户端:ksoap2
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 :(得分:1)
听起来你正试图在主UI线程上做你的请求。因为Web请求需要花费时间,UI线程必须等到它完成才能更新屏幕。
您应该使用AsyncTask在后台执行此请求:
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 -
}
}
使用
启动AsyncTasknew WebRequestTask().execute();
查看http://developer.android.com/reference/android/os/AsyncTask.html了解更多信息