我正在尝试将数据作为 POST 数据发送到服务器。我有 3 个字段,一个是字符串,另一个是 TIMEDATE,最后一个是 BLOB。
我一直在尝试以下代码,但它似乎不起作用。我无法将字节数组转换为字符串,并且不确定如何将字节数组作为参数附加。
如果有人有想法,我将不胜感激!
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://127.0.0.1/add_new_profile");
ProfileData profileData = getProfileData();
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name",profileData.getName()));
nameValuePairs.add(new BasicNameValuePair("created",profileData.getTimeCreated()));
nameValuePairs.add(new BasicNameValuePair("profile_data",new String(profileData.getDataAsByteArray())));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
服务器 PHP 脚本:
<?php
include('connect_db.php');
include('tools.php');
$name = $_POST['name'];
$created = $_POST['created'];
$profile_data = $_POST['profile_data'];
$query = "INSERT INTO fingerprint_profiles(name,created,fingerprint) VALUES ('".$name."','".$created"','".$fingerprint_data."')";
$result = mysql_query($query);
print_as_json($result);
?>