我想從使用json的url加載微調器值。使用json從url加載微調器
我正在使用的代碼是
private class DownloadJSON extends AsyncTask<Void, Void, Void>
{
protected Void doInBackground(Void... params)
{
// Locate the WorldPopulation Class
world = new ArrayList<WorldPopulation>();
// Create an array to populate the spinner
worldlist = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions.getJSONfromURL("http://");
// Log.v("Load Employees",jsonobject.toString());
try
{
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("Employees");
for (int i = 0; i < jsonarray.length(); i++)
{
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setRank(jsonobject.optString("FirstName"));
worldpop.setCountry(jsonobject.optString("LastName"));
worldpop.setPopulation(jsonobject.optString("EmpNo"));
// worldpop.setFlag(jsonobject.optString("flag"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("FirstName") + "-" +jsonobject.optString("EmpNo"));
mySpinner.setAdapter(new ArrayAdapter<String>(VisitorEntry.this,android.R.layout.simple_spinner_dropdown_item, worldlist));
}
}
catch (Exception e)
{
// Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
此代碼有時顯示結果,並且不顯示的某個時候。我不理解這個問題,並想知道是否有其他方式從url中獲取值到微調器。 當我沒有得到與值顯示的時間,我得到以下錯誤:
04-27 14:19:25.979: W/System.err(17486): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-27 14:19:25.979: W/System.err(17486): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4611)
04-27 14:19:25.979: W/System.err(17486): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
04-27 14:19:25.979: W/System.err(17486): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
04-27 14:19:25.979: W/System.err(17486): at android.view.View.invalidate(View.java:10271)
04-27 14:19:25.979: W/System.err(17486): at android.view.View.invalidate(View.java:10226)
04-27 14:19:25.979: W/System.err(17486): at android.widget.AbsSpinner.resetList(AbsSpinner.java:153)
04-27 14:19:25.979: W/System.err(17486): at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:104)
04-27 14:19:25.979: W/System.err(17486): at android.widget.Spinner.setAdapter(Spinner.java:382)
04-27 14:19:25.989: W/System.err(17486): at com.example.wepvms.VisitorEntry$DownloadJSON1.doInBackground(VisitorEntry.java:854)
04-27 14:19:25.989: W/System.err(17486): at com.example.wepvms.VisitorEntry$DownloadJSON1.doInBackground(VisitorEntry.java:1)
04-27 14:19:25.989: W/System.err(17486): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-27 14:19:25.989: W/System.err(17486): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-27 14:19:25.989: W/System.err(17486): at java.lang.Thread.run(Thread.java:856)
你可以用排球爲同一 – Haroon
的JSONObject = JSONfunctions.getJSONfromURL( 「HTTP://」);它是從哪個網址嘗試獲取數據? – Haroon
是的。我有一個網址,我使用和IAM能夠正確地獲取值。 @Haroon – Mounika