gpt4 book ai didi

java - 如何将字符串参数传递给url

转载 作者:行者123 更新时间:2023-11-30 00:12:34 25 4
gpt4 key购买 nike

我需要向这个 URL 传递一个参数 http://192.168.1.15:8888/android_login_api/getsPreferiti.php?id="+mParam1

其中 mParam1 包含此字符串 5a325bc1b214c5.50816853

我该怎么办?

PS:现在我明白了:来自 url 的响应:{"error":false,"message":"VIDEOs fetched successfully.","pdfs":[]} 但 pdfs 数组有 pdfs

最佳答案

使用 HTTPPost 向 HTTPURL 连接添加参数

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("parameter1", parameterValue1));
params.add(new BasicNameValuePair("parameter2", parameterValue2));

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();

private String getQuery(List<NameValuePair> params) throws
UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;

for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}

return result.toString();
}

关于java - 如何将字符串参数传递给url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47927242/

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