我找到了解决方案:
String readFeed = readFeed();
ArrayList<Devices> devices = new ArrayList<Devices>();
ArrayList<String> devicesNames = new ArrayList<String>();
try {
JSONArray jsonArray = new JSONArray(readFeed);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Devices device = new Devices();
device.setName(jsonObject.optString("vhc_name"));
device.setId(jsonObject.optString("vhc_login"));
devices.add(device);
devicesNames.add(jsonObject.optString("vhc_name"));
}
} catch (Exception e) {
}
Spinner mySpinner = (Spinner)findViewById(R.id.myDevicesSpinner);
mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, devicesNames));
}
ReadFeed 是一种从 url 下载 Ids 的方法:
public String readFeed() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String userAuth = Param.getParam("user_Webbsite_Identifier").getValue();
String userPass = Param.getParam("user_Webbsite_password").getValue();
HttpGet httpGet = new HttpGet("http://sce.jelocalise.fr/mobile/ajax/getdevices.php?email="+userAuth+"&password="+userPass+"");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(MyDevices.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
} catch (IOException e)
{
}
return builder.toString();
}