problems with makeHttpRequest

1

Hi chic @ s I'm having problems with this line, it seems to me that makehttprequest is obsolete I've been looking for a lot of info on the internet and I do not find anything, I would appreciate some help thanks

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);
    
asked by adrian ruiz picazo 10.03.2018 в 13:50
source

1 answer

0

Apache classes are obsolete for use on Android:

Warning when building the Gradle of the module

You must use HttpUrlConnection in this way you can perform the POST to your 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();
    }


}

The result would be a 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"}}}]

As an extra comment, to create a ProgressDialog you must use the context of the Activity not the one of the application:

 //dialog = new ProgressDialog(getApplicationContext()); //* INCORRECTO
   dialog = new ProgressDialog(MainActivity.this); //* CORRECTO
    
answered by 12.03.2018 / 18:32
source