Ok so, here is the deal, I've coded an app that requests via HTTP (post) data from a web url, the data is returned using JSon arrays and i parse those arrays to get what i want.
Up until there there's no problem using android 2.3.x but when i test it in Android 4 it just does not work at all.
Here is my code:
public boolean testConexio(){
boolean status = false;
String rutaServer = "URL.php";
//Log.e("log_tag", "Ruta server: "+rutaServer);
InputStream is = null;
String result = "";
String temporal;
//Valors a demanar/enviar
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1",config.getUserDB())); // parametros POST!
nameValuePairs.add(new BasicNameValuePair("param2",config.getPassDB())); // parametros POST!
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(rutaServer);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); // valors POST UTF 8 coded <-- I KNOW! this is the way i need them spanishfag here
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());
}
//Convierte la respuesta a String
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8); // again, spanishfag here
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 la respuesta JSon
try{
JSONObject jArray = new JSONObject(result);
temporal = jArray.getString("status");
if(temporal.equals("true")){
status = true;
}
Log.i("log_tag","Status: "+jArray.getString("status"));
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return status;
}
Can anyone tell me what i'm doing wrong? or what i need to change, i've been searching for a little bit now and i can't make it work on android 4.
THANKS!