gpt4 book ai didi

java - 错误异常 : Already connected In java HttpURLConnection of post method

转载 作者:行者123 更新时间:2023-11-30 02:35:11 24 4
gpt4 key购买 nike

我在这个 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() 以提供可以关闭此连接的提示。

关于java - 错误异常 : Already connected In java HttpURLConnection of post method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26686459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com