1

I am facing problem in this java function i am trying to post data from my table to server but not able to post as i am getting already connected error

Only the first record is posted

  public int myfuction(){

    try{

    String url = "myurl/page.php";
    String urlParameters=null;
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

      Cursor c1 = db.rawQuery("SELECT * FROM temtable ", null);
      String id = null;
      if (c1 != null ) {
            if  (c1.moveToFirst()) {
             do {
                suid = c1.getString(c1.getColumnIndex("puid"));
                urlParameters ="text=STAT=1,DEVICEID=10,TERMINALID="+terminal+",USERID="+id;
               // Send post request
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                //con.disconnect();
                Toast.makeText(this, "Send Para-"+urlParameters, Toast.LENGTH_SHORT).show();
                pstatus = 1;
             }while (c1.moveToNext());
            }
            } 

      con.disconnect();

    Toast.makeText(this, "Send Success", Toast.LENGTH_SHORT).show();
}
    catch(Exception ex)
    {
        Toast.makeText(this, "Error:"+ ex.getMessage(), Toast.LENGTH_SHORT).show();
    }

    return 1;
    }

This is what i am using please help...

Nilesh Solanki
  • 99
  • 3
  • 11

2 Answers2

2

You need to create a new HttpURLConnection via URL.openConnection() inside the loop, every time. You're trying to do connection pooling but HttpURLConnection already does that for you. When you finish with the last iteration, call disconnect() to provide the hint that this connection can be closed.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

I have tried this snippet in while loop itself it work for me and disconnected every time

but this is bad practice to connect and disconnect every single request it loads the server with so many request

String url = "myurl/page.php";
String urlParameters=null;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

so is there any another solution so that only once i can connect and post the data one by one on that connection

Nilesh Solanki
  • 99
  • 3
  • 11
  • No it isn't bad practice, and I have already told you why. It certainly doesn't create any more requests than your original code. – user207421 Nov 01 '14 at 07:02