Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误异常:已在post方法的java HttpURLConnection中连接_Java_Android_Httpurlconnection - Fatal编程技术网

错误异常:已在post方法的java HttpURLConnection中连接

错误异常:已在post方法的java HttpURLConnection中连接,java,android,httpurlconnection,Java,Android,Httpurlconnection,我在这个java函数中遇到了一个问题,我试图将数据从我的表发布到服务器,但无法发布,因为我已经连接了错误 只发布第一条记录 public int myfuction(){ try{ String url = "myurl/page.php"; String urlParameters=null; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openC

我在这个java函数中遇到了一个问题,我试图将数据从我的表发布到服务器,但无法发布,因为我已经连接了错误

只发布第一条记录

  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;
    }

这就是我正在使用的请帮助…

您需要每次通过
URL.openConnection()
在循环内创建一个新的
HttpURLConnection
。您正在尝试进行连接池,但
HttpURLConnection
已经为您完成了这项工作。完成最后一次迭代后,调用
disconnect()
,以提供此连接可以关闭的提示。

我在while循环中尝试了此代码段,它对我有效,并且每次都断开连接

但是,连接和断开服务器上加载的每个请求以及如此多的请求,这是一种糟糕的做法

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");

那么,有没有其他解决方案,只有当我单击按钮事件时,我才能在该连接上一个接一个地连接和发布数据

,获取此11-01 11:20:29.474:V/webview(26555):NO_FAST_DRAW=FALSE 11-01 11:20:29.544:V/webview(26555):singleCursorHandlerTouchEvent-GetEditableSupSupport FASLE 11-01 11:20:32.804:D/dalvikvm(26555):GC_并发释放957K,23%释放7272K/9415K,暂停3ms+4ms@W-I-Z-A-R-D首先在服务器上发布和接收“urlParameters”…但下一步将显示alredy Connected检查此项,然后您需要在发布后关闭连接您要求我连接断开();在while循环中尝试了这个方法,但不起作用。不,这不是一个坏习惯,我已经告诉过你为什么。它肯定不会创建比原始代码更多的请求。