NameValuePair不建议使用openConnection

use*_*387 31 java android basicnamevaluepair

我一直在关注如何从Android应用程序将数据插入数据库的在线教程,除了这个小部分之外,一切都正常

List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Run Code Online (Sandbox Code Playgroud)

NameValuePair和BasicNameValuePair已被弃用,有利于openConnection().如何创建与之相关的新值关联?http://developer.android.com/reference/java/net/URL.html#openConnection()

有谁知道如何使用openConnection创建名称值对?我到处搜寻.

fri*_*mle 45

我刚遇到同样的问题.已弃用的类org.apache.http已在API中删除23.

我最终使用了android.util.Pair.它工作得很好,代码也很短:

List<Pair<String, String>> params = new ArrayList<>();
params.add(new Pair<>("username", username));
params.add(new Pair<>("password", password));
Run Code Online (Sandbox Code Playgroud)


Men*_*ami 28

您可以使用ContentValues,例如:

ContentValues values = new ContentValues();
values.put("username", name);
values.put("password", password);
database.insert(Table_name, null, values);
Run Code Online (Sandbox Code Playgroud)

  • "ContentValues"的设计是为了由内容解析器使用而不是网络通信 - 问题所依据的 - 因为在你需要解析头属性时没有`getName()`和`getValue()`方法. (10认同)

Fah*_*him 7

替换为NameValuePair.您还可以从中获取其名称和值,如下所述.这里的关键是名字.

创建:

ContentValues values = new ContentValues();
values.put("key1", "value1");
values.put("key2", "value2");
Run Code Online (Sandbox Code Playgroud)

获得关键和价值:

for (Map.Entry<String, Object> entry : values.valueSet()) {
    String key = entry.getKey(); // name
    String value = entry.getValue().toString(); // value
}
Run Code Online (Sandbox Code Playgroud)