传递参数与HttpURLConnection类

编程入门 行业动态 更新时间:2024-10-17 15:22:07
本文介绍了传递参数与HttpURLConnection类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

随着API 22 pcated老阿帕奇东西德$ P $,我终于得到周围更新我的网络的东西。

With the old Apache stuff deprecated in API 22, I am finally getting around to updating my network stuff.

使用的openConnection()似乎pretty直线前进。不过,我还没有看到任何很好的例子,与发送的参数。

Using openConnection() seems pretty straight forward. However, I have not seen any good example to send parameters with it.

我怎么会更新这个code?

How would I update this code?

ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); param.add(new BasicNameValuePair("user_id",String.valueOf(userId))); httpPost.setEntity(new UrlEncodedFormEntity(param));

现在的NameValuePair 和 BasicNameValuePair 也在德precated。

Now that NameValuePair and BasicNameValuePair are also deprecated.

编辑:我的服务器端 PHP code不希望JSON参数,我不能所有的突然切换,由于现有用户的 - 所以建议非JSON的答案。

My server side php code doesn't expect JSON parameters and I can't all of the sudden switch due to existing users -- so a non-JSON answer is recommended.

EDIT2:我只需要在一瞬间目标于Android 4.1

I just need to Target Android 4.1+ at the moment.

推荐答案

我固定它是这样的:

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

下面是参数的东西:

String charset = "UTF-8"; String s = "unit_type=" + URLEncoder.encode(MainActivity.distance_units, charset); s += "&long=" + URLEncoder.encode(String.valueOf(MainActivity.mLongitude), charset); s += "&lat=" + URLEncoder.encode(String.valueOf(MainActivity.mLatitude), charset); s += "&user_id=" + URLEncoder.encode(String.valueOf(MyndQuest.userId), charset); conn.setFixedLengthStreamingMode(s.getBytes().length); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.print(s); out.close();

更多推荐

传递参数与HttpURLConnection类

本文发布于:2023-07-14 19:55:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   HttpURLConnection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!