So here is the deal, I encoded an application that requests data from the web url via HTTP (post), the data is returned using JSon arrays, and I parse these arrays to get what I want.
As long as there is no problem using Android 2.3.x, but when I test it on Android 4, it just doesn't work.
Here is my code:
public boolean testConexio(){
boolean status = false;
String rutaServer = "URL.php";
InputStream is = null;
String result = "";
String temporal;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1",config.getUserDB()));
nameValuePairs.add(new BasicNameValuePair("param2",config.getPassDB()));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(rutaServer);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
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());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),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());
}
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 someone tell me what I am doing wrong? or what I need to change, I was looking a bit now, and I can’t get it to work on Android 4.
THANK!
source
share