0

我必须从这个 php 页面填充一个微调器: http ://sce.jelocalise.fr/mobile/ajax/getdevices.php? email=info@mediacom-i.fr&password=thedevil1 我已经阅读了很多关于微调器的教程和填充它们的方式,但我未能在我的代码中调整它们。这是它在 iPhone 上的样子(我必须将 iPhone 应用程序改编成 Android 版本):http: //img15.hostingpics.net/pics/665348IMG0017.png

我必须在我的微调器中只显示“vhc_name”并检索“vhc_login”和“vhc_password”来连接用户。我已经建立了一个有效的连接,我只需要检索好的 ID 来连接用户。任何帮助将不胜感激。

4

1 回答 1

0

我找到了解决方案:

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();
}
于 2013-05-17T07:04:43.307 回答