Java 已弃用 openConnection 的 NameValuePair
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29378012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameValuePair deprecated for openConnection
提问by user1949387
I been following online tutorials on how to insert data into a database from an android app and have everything working except this small part
我一直在关注有关如何将数据从 android 应用程序插入数据库的在线教程,并且除了这一小部分之外,所有东西都可以正常工作
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
NameValuePair and BasicNameValuePair have been deprecated in favor of openConnection()
. How can I create a new-value association with that? http://developer.android.com/reference/java/net/URL.html#openConnection()
NameValuePair 和 BasicNameValuePair 已被弃用,而支持openConnection()
. 我怎样才能用它创建一个新的价值关联?http://developer.android.com/reference/java/net/URL.html#openConnection()
Does anyone know how I can create name value pairs with openConnection? I been searching everywhere.
有谁知道如何使用 openConnection 创建名称值对?我到处寻找。
采纳答案by Menna-Allah Sami
You can use ContentValues
, for example:
您可以使用ContentValues
,例如:
ContentValues values = new ContentValues();
values.put("username", name);
values.put("password", password);
database.insert(Table_name, null, values);
回答by Ahmad Hasan
Yo can change your android Api in 21, right click, properties android, click in api 21 is work for me
你可以在 21 中更改你的 android Api,右键单击,属性 android,在 api 21 中单击对我有用
回答by Dipankar Baghel
You can use httpmime.jar file instead of it, which will work better that NameValuePair. You can Download it from here, Download
您可以使用 httpmime.jar 文件代替它,它会比 NameValuePair 更好地工作。你可以从这里下载,下载
MultipartEntity multi = new MultipartEntity();
multi.addPart("name", new StringBody("Raj"));
multi.addPart("Id", new StringBody("011"));
add this jar to your project and then use it.
将此 jar 添加到您的项目中,然后使用它。
回答by Fahim
Alternate to NameValuePair. Also you can get the name and values from it as mentioned below. Here key isa name.
替代 NameValuePair。您也可以从中获取名称和值,如下所述。这里关键是一个名字。
Create:
创建:
ContentValues values = new ContentValues();
values.put("key1", "value1");
values.put("key2", "value2");
Get key and value :
获取键和值:
for (Map.Entry<String, Object> entry : values.valueSet()) {
String key = entry.getKey(); // name
String value = entry.getValue().toString(); // value
}
回答by friederbluemle
I just ran into the same problem. The deprecated classes from org.apache.http
have been removedin API 23
.
我刚刚遇到了同样的问题。org.apache.http
已在 API 中删除了已弃用的类23
。
I ended up using android.util.Pair
. It works perfectly, and the code is shorter too:
我最终使用了android.util.Pair
. 它工作得很好,代码也更短:
List<Pair<String, String>> params = new ArrayList<>();
params.add(new Pair<>("username", username));
params.add(new Pair<>("password", password));
回答by thilina c
If you realy want to use NameValuePair
in your application, you can add the useLibrary 'org.apache.http.legacy'
to your gradle:
如果你真的想NameValuePair
在你的应用程序中使用,你可以将它添加useLibrary 'org.apache.http.legacy'
到你的 gradle 中:
buildtypes{
//------------
//----------
}
useLibrary 'org.apache.http.legacy'
回答by ked
you can use contentvalues or hashmap based on your preference.
您可以根据自己的喜好使用 contentvalues 或 hashmap。
i have used Content values
我使用了内容值
ContentValues contentValues = new ContentValues();
contentValues.put("key1","value1");
contentValues.put("key2","value2");
and if the data that u are posting is form data then here`s how u can convert it form data
如果您发布的数据是表单数据,那么您可以通过以下方式将其转换为表单数据
public String getFormData(ContentValues contentValues) throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Map.Entry<String, Object> entry : contentValues.valueSet()) {
if (first)
first = false;
else
sb.append("&");
sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
sb.append("=");
sb.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
}
return sb.toString();
}