我能够从 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;
}
所以现在我不确定如何将身份验证器密码/登录名附加到请求并发送到服务器。知道我怎样才能做到这一点吗?
谢谢!
最佳答案
你只需这样做
final URL url = new URL(myUrl+"?login="+myEmail+"&password="+myPassword);
并且您不需要 setRequestProperty
行。这些实际上是在设置 Http 请求的属性,而不是查询参数。
关于android - 如何将请求参数附加到当前 Android HttpUrlConnection 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9489037/