0

I'm new in android and had tried to find any refer/tutorial on internet but not yet fixing my issues

I have this code to show the value :

List<Contact> contacts = db.test();
for (Contact cn : contacts) {
    String log = "Audit Name "+cn.getName()+" ,CountAudit " + cn.getAudit()+" ,CountFlag "
            + cn.getFlag();
    if(String.valueOf(cn.getFlag()).equals(cn.getAudit())){
        Log.d("Berhasil", "Sama Cok");
    }
    Log.d("Name: ", log);
}

the logcat shows : Audit Name 12447 ,CountAudit 1 ,CountFlag 1

now I need to create HttpURLConnection that can stored Audit Name value to API

I'm creating new class :

private class sendget extends AsyncTask<Void, String, Void >
{
    @Override
    protected Void doInBackground(String audit) {

        try {

            URL url = new URL("myurl"+"?"+ audit);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("Accept", "application/json");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
            os.writeBytes(audit);

            os.flush();
            os.close();

            conn.disconnect();
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
}

but I've too much error,

My Goal is to using this code :

List<Contact> contacts = db.test();
    for (Contact cn : contacts) {
        String log = "Audit Name "+cn.getName()+" ,CountAudit " + cn.getAudit()+" ,CountFlag "
                + cn.getFlag();
        if(String.valueOf(cn.getFlag()).equals(cn.getAudit())){
            // add this code to calling httpurlconnection class with parameter from cn.getName()
            sendget.execute(cn.getName);
            // so the complete url gonna be myurl?audit=12447
            Log.d("Berhasil", "Sama Cok");
        }
        Log.d("Name: ", log);
    }

Kindly someone guide me to create HttpURLConnection class that work to above code, any tutorial would be glad

flix
  • 1,688
  • 3
  • 34
  • 64

1 Answers1

0

doInBackground requires variable arguments. For example,

private class sendget extends AsyncTask<Void, String, Void >
{
    @Override
    protected Void doInBackground(String... audit) {
        URL url = new URL("myurl"+"?"+ audit[0]);

However, if you are sending JSON data, you should 1) consider sending a whole array at once rather than individual values (your device battery will thank you), and 2) using a different networking library. Writing AsyncTasks (correctly) is basically a waste of time.

By the way, if you are trying to implement a database on your phone to sync with the web server database - you're going to run into a lot of problems when other devices update the web server outside of your own app. How do you get notified of changes?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • actually, im creating android code to using it on react-native, so i dont know how to use another network lib in react-native – flix Nov 29 '17 at 04:09
  • You are already using Java code. Gradle is probably including all the necessary libraries for your Android project. Add them there. It isn't about React – OneCricketeer Nov 29 '17 at 04:10
  • no it's not about react, just in case my problem running in android, i can convert those code to react, using native module – flix Nov 29 '17 at 04:14