Ciao a tutti,
sto sviluppando la mia prima applicazione Android; in tal senso sto richiamando una pagina PHP che effettua un'interrogazione ad un database e mi restituisce un oggetto JSON che poi voglio gestire nella mia applicazione.
A tal proposito ho scritto il codice prendendo spunto da un esempio; questo il codice interessato:
codice:
public void RecuperaInfo()
{
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cap", "08026"));
InputStream is;
//http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost/xampp/arganet/webservice.php?cap=08026");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "error in http connection "+ e.toString());
}
//Conversione responso in stringa
try
{
BufferedReader reader = new BufferedReader(newInputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length(); i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "id: " + json_data.getInt("idComuni"
) +
", comune: " + json_data.getString("Comune") +
", cap: " + json_data.getString("CAP")
);
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
Mi viene segnalato un errore sull'istruzione
codice:
BufferedReader reader = new BufferedReader(newInputStreamReader(is, "iso-8859-1"), 8);
L'errore è il seguente:is cannot be resolved to a variable
Da quello che ipotizzo chiaramente la variabile is è dichiarata in un blocco try che non consente giustamente di poterla gestire all'esterno di questo blocco.
Allorchè ho provato a dichiarare esternamente questa variabile, così da poter essere riconosciuta da tutti i blocchi try, ma in questo caso ho sempre un errore:
codice:
The method newInputStreamReader(InputStream, String) is undefined for the type Act2
Come è possibile ciò?