gpt4 book ai didi

android - 如何将请求参数附加到当前的 Android HttpUrlConnection 调用

转载 作者:搜寻专家 更新时间:2023-11-01 09:09:20 25 4
gpt4 key购买 nike

我可以从 Android 程序向远程服务器发送请求,但请求似乎没有参数。如何附加参数?

这是我当前的代码:

    @Override
protected String doInBackground(String... theParams)
{

String myUrl = theParams[0];
final String myEmail = theParams[1];
final String myPassword = theParams[2];

Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication( myEmail, myPassword.toCharArray());
}
});

String response = null;

try
{
final URL url = new URL(myUrl);

final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("login", myEmail);
conn.setRequestProperty("password", myPassword);

conn.setUseCaches(false);

final InputStream is = conn.getInputStream();
final byte[] buffer = new byte[8196];
int readCount;
final StringBuilder builder = new StringBuilder();
while ((readCount = is.read(buffer)) > -1)
{
builder.append(new String(buffer, 0, readCount));
}

response = builder.toString();
Log.d( "After call, response: " , " " + response);
}
catch (Exception e)
{

}

return response;
}

所以现在我不确定如何将 Authenticator 密码/登录名附加到请求并发送到服务器。知道如何实现吗?

谢谢!

最佳答案

你只需这样做

final URL url = new URL(myUrl+"?login="+myEmail+"&password="+myPassword);

而且您不需要 setRequestProperty 行。这些实际上是在设置您的 Http 请求的属性,而不是查询参数。

关于android - 如何将请求参数附加到当前的 Android HttpUrlConnection 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9489037/

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