Olá galera, preciso de uma pequena ajuda no meu codigo… está dando erro no primeiro Try.
final List<String> list1 = new ArrayList<String>();
try{
URL urlspinner = new URL("http://homecar.16mb.com/android/spinnercarro2.php");
urlConnection = (HttpURLConnection) urlspinner.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
}catch (Exception e){
Log.e("Fail 3", e.toString());
Toast.makeText(getApplicationContext(),"algo errado", Toast.LENGTH_LONG).show();
finish();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) !=null){
sb.append(line + "/n");
}
is.close();
result = sb.toString();
}catch (Exception e){
Log.e("Fail 2", e.toString());
}
try{
JSONArray JA = new JSONArray(result);
JSONObject json = null;
name = new String[JA.length()];
for(int i=0;i<JA.length();i++){
json=JA.getJSONObject(i);
name[i]=json.getString("descricao");
}
Toast.makeText(getApplicationContext(),"sss", Toast.LENGTH_LONG).show();
for(int i=0;i<name.length;i++){
list1.add(name[i]);
}
Toast.makeText(getApplicationContext(),"len", Toast.LENGTH_LONG).show();
spinner_fn();
}catch (Exception e){
Log.e("Fail 3",e.toString());
}
Segue o método:
private void spinner_fn(){
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(TelaLocar.this, android.R.layout.simple_spinner_item, name);
spinnerLocarCarro.setAdapter(dataAdapter1);
spinnerLocarCarro.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id){
spinnerLocarCarro.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0){
}
});
}
codigo php
<?php
include_once 'conexao2.php';
$sql = $dbcon->query("SELECT * FROM veiculo WHERE status='y'");
while($dados = mysqli_fetch_assoc($sql)){
$tmp[] = $dados['descricao'];
}
echo json_encode($tmp);
?>