android– 如何使用NameValuePair使用POST向HttpURLConnection添加参数

android– 如何使用NameValuePair使用POST向HttpURLConnection添加参数,第1张

概述我正在尝试使用HttpURLConnection进行POST(我需要以这种方式使用它,不能使用HttpPost)并且我想将参数添加到该连接中,例如post.setEntity(newUrlEncodedFormEntity(nvp));哪里nvp=newArrayList<NameValuePair>();有一些数据存储在.我找不到如何将这个ArrayList添加到我

我正在尝试使用httpURLConnection进行POST(我需要以这种方式使用它,不能使用httpPost)并且我想将参数添加到该连接中,例如

post.setEntity(new UrlEncodedFormEntity(nvp));

哪里

nvp = new ArrayList<@R_419_6889@ValuePair>();

有一些数据存储在.我找不到如何将这个ArrayList添加到我的httpURLConnection的方法,这是在这里:

httpsURLConnection https = (httpsURLConnection) url.openConnection();https.setHost@R_419_6889@VerifIEr(DO_NOT_VERIFY);http = https;http.setRequestMethod("POST");http.setDoinput(true);http.setDoOutput(true);

这种尴尬的https和http组合的原因是不需要验证证书.但这不是问题,它可以很好地发布服务器.但是我需要用论据发帖.

有任何想法吗?

重复免责声明:

回到2012年,我不知道如何将参数插入到http POST请求中.我在@R_419_6889@ValuePair上,因为它是在教程中.这个问题可能看似重复,但是,我的2012年自己阅读了other问题并且它没有使用@R_419_6889@ValuePair.事实上,它并没有解决我的问题.

解决方法:

您可以获取连接的输出流并将参数查询字符串写入其中.

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<@R_419_6889@ValuePair> params = new ArrayList<@R_419_6889@ValuePair>();params.add(new Basic@R_419_6889@ValuePair("firstParam", paramValue1));params.add(new Basic@R_419_6889@ValuePair("secondParam", paramValue2));params.add(new Basic@R_419_6889@ValuePair("thirdParam", paramValue3));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<@R_419_6889@ValuePair> params) throws UnsupportedEnCodingException{    StringBuilder result = new StringBuilder();    boolean first = true;    for (@R_419_6889@ValuePair pair : params)    {        if (first)            first = false;        else            result.append("&");        result.append(URLEncoder.encode(pair.get@R_419_6889@(), "UTF-8"));        result.append("=");        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));    }    return result.toString();}
总结

以上是内存溢出为你收集整理的android – 如何使用NameValuePair使用POST向HttpURLConnection添加参数全部内容,希望文章能够帮你解决android – 如何使用NameValuePair使用POST向HttpURLConnection添加参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104568.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存