我是一个新手,试图通过 xampp 将数据从 Android 应用程序发送到在本地主机服务器上设置的 MySQL 数据库。此方法应该将姓名、用户名、密码、电话号码和年龄放入 NameValuePair 类型的数组列表中。
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("age", user.age + ""));
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
dataToSend.add(new BasicNameValuePair("phoneNumber", user.phoneNumber));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost("localhost/Register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
当我运行应用程序时,我收到以下异常:
Target host must not be null, or set in parameters. scheme=null, host=null, path=localhost/Register.php
我的研究使我相信 url 字符串有问题,但我不知 Prop 体是什么问题。
提前致谢。
(附:我也知道在数据库中以纯文本形式存储密码的可怕安全隐患。只是想学习如何在应用程序和数据库之间进行交互。谢谢)
最佳答案
如果本地主机替换对您不起作用,请尝试使用此代码发布数据。
HttpParams params = new DefaultHttpParams(); // setup whatever params you what
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost("someurl");
post.setEntity(new UrlEncodedFormEntity()); // with list of key-value pairs
client.execute(post, new ResponseHandler(){}); // implement ResponseHandler to handle response correctly.
关于java - 将数据从 Android 应用程序发送到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30770960/