1

Hola chic@s estoy teniendo problemas con esta linea , me parece que makehttprequest esta obsoleta he estado buscando mucha info por internet y no encuentro nada, agradeceria algo de ayuda gracias

public class MainActivity extends AppCompatActivity {

    private ProgressDialog dialog;
    ListView lv;
    JSONParser jParser = new JSONParser();
    ArrayList<HashMap<String, String>> productsList;
    private static String url = "http://192.168.2.19/android/control/jquery.php";

    private static final String imagen = "imagen";
    private static final String posicion = "posicion";
    private static final String hora = "hora";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        productsList=new ArrayList<HashMap<String, String>>();
        JSONArray products = null;
        new LoadAllProducts().execute();
        lv = findViewById(R.id.list);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast toas=Toast.makeText(getApplicationContext(),lv.getChildCount(),Toast.LENGTH_SHORT);
                toas.show();
            }
        });

    }



    class LoadAllProducts extends AsyncTask<String,String,String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(getApplicationContext());
            dialog.setMessage("Loading products. Please wait...");
            dialog.setIndeterminate(false);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected String doInBackground(String... strings) {
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            JSONObject json = jParser.makeHttpRequest(url, "POST", nameValuePair);

            return null;
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }


    }

}

JSONObject json = jParser.makeHttpRequest(url, "POST", nameValuePair);

1 respuesta 1

0

Las clases Apache son obsoletas para su uso en Android:

Warning al construir el Gradle del modulo

Debes usar HttpUrlConnection de esta forma puedes realizar el POST a tu url:

private ProgressDialog dialog;
private static String url = "https://api.myjson.com/bins/y76il";
private StringBuilder response;

class LoadAllProducts extends AsyncTask<String,String,String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading products. Please wait...");
        dialog.setIndeterminate(false);
        dialog.setCancelable(false);
        dialog.show();
    }


    @Override
    protected String doInBackground(String... strings) {

        try {
            URL myUrl = new URL(url);
            HttpURLConnection urlConnection = null;

            urlConnection = (HttpURLConnection) myUrl.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            //response = in.toString();
            BufferedReader r = new BufferedReader(new InputStreamReader(in));
            response = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                response.append(line);
            }

            urlConnection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return response.toString();
    }


    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Log.d(TAG, "Se obtuvo el resultado: " + s);
        dialog.dismiss();
    }


}

El resultado sería un JsonArray:

[{"id":1001,"name":"Super1","user":{"name":"The Super 1"},"items":{"987987M7812b163eryrt":{"id":1,"strong":456,"active":true,"sell":"te"},"90812bn120893juuh":{"id":2,"strong":4700,"active":true,"sell":"tt"},"981273jn19203nj123rg":{"id":3,"strong":3000,"active":true,"sell":"ti"}}}]

Como comentario extra, para crear un ProgressDialog debes usar el contexto de la Activity no el de la aplicación:

 //dialog = new ProgressDialog(getApplicationContext()); //* INCORRECTO
   dialog = new ProgressDialog(MainActivity.this); //* CORRECTO

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.