不要如何替换BasicValueNamePair作为其弃用

时间:2015-04-09 02:27:59

标签: android

这是我下面的代码,尝试使用PHP,mysql和xampp服务器创建登录页面。我对BasicValueNamePair有问题,因为它已被弃用。我不知道如何用新代码替换。请帮忙

URL url = new URL("http://10.0.3.2/android_api/check.php");
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.connect();

InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String res = IOUtils.toString(in, "UTF-8");
System.out.println(res + " Blu bluh");

// make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name
// and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("Username",username.getText().toString().trim()));
//$Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("Password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);

1 个答案:

答案 0 :(得分:0)

HttpURLConnection是从URLConnection派生的,所以你可以 使用addRequestProperty来包含你的名字 - 值对。

urlConnection.addRequestProperty("Username",username.getText().toString());

查看详情here