JSONArray ["category_1","category_2","category_3"] がありますが、データを取得できません。Androidでこのデータをスピナーまたはリストビューに設定する方法。
E:
障害はどこにありますか?
public class MainActivity extends Activity {
String urlCat = "http://tvapp.pcrevue.sk/categories.json";
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
JSONArray jsonArray;
jsonArray = getJSONArrayFromUrl(urlCat);
final String[] array_spinner = new String[jsonArray.length()];
//int show_total = jsonArray.length();
//Toast.makeText(flash_tattoo.this, show_total + "test", Toast.LENGTH_LONG).show();
for (int i=0; i<jsonArray.length(); i++)
{
String styleValue = jsonArray.getJSONArray(0).getString(i);
array_spinner[i] = styleValue;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,array_spinner);
adapter.setDropDownViewResource(R.layout.activity_main);
spinner.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public JSONArray getJSONArrayFromUrl(String url) {
InputStream is = null;
JSONArray jObj = null;
String json = "";
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
//json += line;
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}