HTTP POST请求的Andr​​oid 4.0(2.3工作)? [英] HTTP POST request ANDROID 4 (working in 2.3)?

查看:115
本文介绍了HTTP POST请求的Andr​​oid 4.0(2.3工作)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以,这里的交易,我已经codeD的应用程序,通过HTTP(POST)数据从Web URL请求,该数据是使用JSON数组返回,我分析这些阵列来获得我想要什么

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.

在此之前有没有使用的Andr​​oid 2.3.x是没有问题的,但是当我在安卓4.0测试它是行不通的。

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.

下面是我的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;
}

谁能告诉我什么,我做错了吗?或者我需要改变,我一直在寻找一点点,现在我不能让它在Android 4的工作。

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.

谢谢!

推荐答案

您需要把它放到一个异步任务,在3.0中的主线程它不会运行>

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

使用这样的:

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] + " ");
                }

        }
    }

您需要引用它的异步任务外

You need a reference to it outside the async task

PostTask posttask;

那么你需要启动

then you need to start it

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

我一个前几天有完全相同的问题,古德勒克

I had the exact same problem a couple of days ago, goodluck

这篇关于HTTP POST请求的Andr​​oid 4.0(2.3工作)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆