我想从我的网站获取数据,然后将这些信息放入微调器中。我已经使用了一些教程,我想出了这段代码
package com.thenewboston.christian;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class hospitalhttp extends Activity{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
private static String url_all_products = "http://www.photosbychristian.com/ems/hospitals.php";
//private static final String TAG_HID = "hid";
private static final String TAG_HOSPITAL = "hospital";
JSONArray hosps = null;
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item );
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnertest);
new LoadAllProducts().execute();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(hospitalhttp.this);
pDialog.setMessage("Loading hospitals. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
try {
// products found
// Getting Array of Products
hosps = json.getJSONArray("PA");
// looping through All Products
for (int i = 0; i < hosps.length(); i++) {
JSONObject c = hosps.getJSONObject(i);
// Storing each json item in variable
//String id = c.getString(TAG_HID);
String name = c.getString(TAG_HOSPITAL);
adapter.add(name);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
*/
Spinner state = (Spinner) findViewById(R.id.spinner1);
state.setAdapter(adapter);
}
});
}
}
}
我尝试运行我的应用程序并收到此错误
02-22 22:29:48.131: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thenewboston.christian/com.thenewboston.christian.hospitalhttp}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
所以我在oncreate bolck之后移动所有这些,我仍然得到相同的错误
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
private static String url_all_products = "http://www.photosbychristian.com/ems/hospitals.php";
//private static final String TAG_HID = "hid";
private static final String TAG_HOSPITAL = "hospital";
JSONArray hosps = null;
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item );
所以我不知道该怎么做,知道任何帮助将不胜感激
答案 0 :(得分:3)
移动代码无济于事。 Java是一种范围内的语言,如果在方法之后或之前放置某些东西并不重要。它仍然在同一范围内。
这条线可能引发了这个问题:
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item );
您希望在对象范围初始值设定项中加载布局。这种代码甚至在构造函数之前运行。您必须将值赋值移动到onCreate
方法中,如下所示:
ArrayAdapter <String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnertest);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
new LoadAllProducts().execute();
}
答案 1 :(得分:0)
所以我在oncreate bolck之后移动所有这些,我仍然得到相同的错误
当您声明静态值时,该代码不能在方法内部。因此,该代码必须位于任何方法之外,这意味着它将在任何方法之前执行,因此将在onCreate()
之前执行。
那里的某些内容 - 可能是您的JSONParser
- 正在尝试做一些在{em> super.onCreate()
执行完{em> onCreate()
之前无法完成的事情。super.onCreate()
方法。您的Java堆栈跟踪将具体告诉您哪些行(如果有)是您的难点的来源。然后,您需要在调用 onCreate()
之后执行该代码,例如将其放在{{1}}方法的末尾。