我从php获取一些数据并尝试通过arrayadater在spinner中显示该数据,但是有一个概率可以
**DownloadDetails downloaddeatils = new DownloadDetails(this);
downloaddeatils.execute(url);
public class DownloadDetails extends AsyncTask<String, Void, String> {
Context context;
ProgressDialog progressDialog;
public DownloadDetails(Context context) {
this.context = context;
progressDialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Fetching Data, please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... urls) {
String output = null;
for(String url: urls) {
output = getOutputFromUrl(url);
}
return output;
}**
private String getOutputFromUrl(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
JSONObject object = null;
JSONObject object1 = null;
JSONObject object2 = null;
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("speciality_id", sid));
nameValuePairs.add(new BasicNameValuePair("type",type));
nameValuePairs.add(new BasicNameValuePair("condition_id", cid));
nameValuePairs.add(new BasicNameValuePair("locality", locality));
nameValuePairs.add(new BasicNameValuePair("city_name", city_name));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpPost,
responseHandler);
Log.d("Respone",responseBody);
if(!responseBody.equals(null))
{
JSONObject jsonResponse = new JSONObject(responseBody);
object = jsonResponse.getJSONObject("firstarray");
object1 = jsonResponse.getJSONObject("secondarray");
object2 = jsonResponse.getJSONObject("thirdarray");
try {
if (!object.toString().trim().contains("null")) {
Log.d("Inside ","count"+count);
if (count == 0) {
JSONArray jArray = object.getJSONArray("values");
for (int i = 0; i < jArray.length(); i++) {
String str = jArray.getString(i).substring(0,
jArray.getString(i).length());
String[] data = str.split("_");
specialistList.add(data[0]);
specialistNameList.add("" + data[1]);
//specialistAdapter.add("" + data[1]);
}
//specialistAdapter.setNotifyOnChange(true);
count = 1;
}
}
if (!object1.toString().trim().contains("null")) {
JSONArray JArray1 = object1.getJSONArray("values1");
for (int i = 0; i < JArray1.length(); i++) {
String str = JArray1.getString(i).substring(0,
JArray1.getString(i).length());
String[] data = str.split("_");
conditionList.add(data[0]);
//conditionAdapter.add(data[1]);
conditionNameList.add(data[1]);
}
//conditionAdapter.setNotifyOnChange(true);
Log.d("Inside","SecondArray");
}
if (!object2.toString().trim().contains("null")) {
JSONArray JArray1 = object2.getJSONArray("values2");
for (int i = 0; i < JArray1.length(); i++) {
String str = JArray1.getString(i).substring(0,
JArray1.getString(i).length());
String[] data = str.split("_");
doctorIdList.add(data[0]);
doctorNameList.add(data[1]);
// adapter.add(data[1]);
}
// adapter.setNotifyOnChange(true);
}
} catch (Exception e) {
Log.d("Unknown exception",""+e);
e.printStackTrace();
}
}
else
{
Toast.makeText(getApplicationContext(),"Unable to fetch data from server",Toast.LENGTH_LONG).show();
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String output) {
super.onPostExecute(output);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
for(int i=0;i<specialistNameList.size();i++)
specialistAdapter.add(specialistNameList.get(i));
for(int j=0;j<conditionNameList.size();j++)
conditionAdapter.add(conditionNameList.get(j));
for(int k=0;k<doctorNameList.size();k++)
adapter.add(doctorNameList.get(k));
}
if(output != null) {
Toast.makeText(getApplicationContext(), "Cannot connect to Server",Toast.LENGTH_LONG).show();
}
else {
}
}
}
答案 0 :(得分:1)
您应该在onPostExecute()
或onPreExecute()
或onPublishedProgress()
方法中编写修改事件线程的代码,而不是doInBackground()
方法。