gpt4 book ai didi

java - 从 Android 应用程序发送 POST

转载 作者:行者123 更新时间:2023-12-02 05:54:36 24 4
gpt4 key购买 nike

我正在尝试向服务器发送 POST 请求。以下java代码适用于PC应用程序,但不适用于我的Android应用程序。添加了 Iternet 权限,那么,我必须做什么才能发送这篇文章,以及我必须使用哪些其他方法和库来替换 Android 的此代码?

      String hostname = "xxxxxxx.box"; 
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket sock = new Socket(addr, port);
String SID = new classSID("xxxxxx").obtainSID();
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
String str = "enabled=on&username="+givenName+"&email="+givenEmail+"&password="+givenPassword+"&frominternet=on&box_admin_rights=on&phone_rights=on&homeauto_rights=on&uid=&sid="+SID+"&apply=";

////////////////////////////////////////////////////////////////////////////////////////////
wr.write("POST /system/boxuser_edit.lua HTTP/1.1");
wr.write("Host: xxxxxx:80" + "\r\n");
wr.write("Accept: text/html" + "\r\n");
wr.write("Keep-Alive: 300" + "\r\n");
wr.write("Connection: Keep-Alive" + "\r\n");
wr.write("Content-Type: application/x-www-form-urlencoded"+"\r\n");
wr.write("Content-Length: "+str.length()+"\r\n");
wr.write("\r\n");
wr.write(str+"\r\n");
wr.flush();
////////////////////////////////////////////////////////////////////////////////////////////
BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream(),"UTF-8"));
String line;
while((line = rd.readLine()) != null)
Log.v("Response", line);

wr.close();
rd.close();
sock.close();
}

最佳答案

问题在于在端口 80 上创建套接字。您需要 root 权限才能访问 android 中的端口 < 1024。请参阅issue

关于java - 从 Android 应用程序发送 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23216211/

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