HTTP POST ANDROID 4 request (works in version 2.3)?

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";
    //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 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!

+3
source share
2 answers

You need to put this in an Async task, it will not work in the main thread in 3.0>

use this:

public class PostTask extends AsyncTask<Void, String, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean result = false;

            //All your code goes in here 

            //If you want to do something on the UI use progress update

            publishProgress("progress");
            return result;
        }

        protected void onProgressUpdate(String... progress) {
            StringBuilder str = new StringBuilder();
                for (int i = 1; i < progress.length; i++) {
                    str.append(progress[i] + " ");
                }

        }
    }

async

PostTask posttask;

posttask = new PostTask();
posttask.execute();

, goodluck

+7

Android 4.0 http- Thread ( runnable, asyinctask...)

, , Thread, , : targetVersion = "14".

14/, Holo -,

→ Propierties → Android → Project Built Target = 14

+1

All Articles