我需要什么:我需要从android应用程序连接到ftp服务器,因为我想从我的android应用程序将图像上传到该服务器,并且我也想将一些数据(字符串...)上传到该服务器上的mySQL DB。
问题是:如果我想连接到ftp,我需要在Java代码中写我的登录名并传递访问我的FTP或登录mySQL DB的权限,如这些示例> source 1和source 2,但是我不想写我的登录名,传入Java代码,因为任何人都可以撤消我的应用程序,然后他可以编辑我的数据库atc,这不是好方法。
问题是:如何或可以使用什么进行身份验证?
我正在使用什么:我正在将Apache服务器与php和mySQL db一起使用。
我将不胜感激。
最佳答案
我有一个树莓派,作为运行Apache,PHP和MySQL DB的小型服务器运行,以保存在Android设备上运行的GCM应用程序的registrationID。注册设备后,它将regID发送到DB。
该表称为注册,它具有设备,项目,所有者,注册ID列。
我应该强调,Android手机上的以下代码全部包装在AsyncTask中,并在doInBackground方法中调用。
String script = "register_phone.php";
String serverUrl = SERVER_URL + script;
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("devId", devId);
params.put("projId", projId);
params.put("owner", owner);
// some other largely irrelevant stuff
post(serverUrl, params);
private static void post(String endpoint, Map<String, String> params)
throws IOException {
String url = null;
url = endpoint;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
String key = param.getKey();
String value = param.getValue();
nameValuePairs.add(new BasicNameValuePair(key, value));
}
HttpResponse response;
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, NETWORK_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, NETWORK_TIMEOUT);
HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response = httpClient.execute(httpPost);
//int status = httpClient.getResponseCode();
Log.d(GCMTAG, "response " + response.getEntity());
String responseBody = "empty";
responseBody = EntityUtils.toString(response.getEntity());
Log.d(GCMTAG, "responsebody " + responseBody);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e){
e.printStackTrace();
}
}
}
在服务器上,我有:
<?php
require 'vars.php';
$projid=$_POST['projId'];
$devid=$_POST['devId'];
$owner=$_POST['owner'];
$regid=$_POST['regId'];
//echo $owner; echo $projid; echo $devid;
$mysqli = new mysqli("localhost", $user, $pwd, $database);
$regidIN = mb_convert_encoding($regid, "UTF-8");
$projectIN = mb_convert_encoding($projid, "UTF-8");
$deviceIN = mb_convert_encoding($devid, "UTF-8");
$ownerIN = mb_convert_encoding($owner, "UTF-8");
$queryone="replace into registrations (device, project, owner, regid)
values ('$deviceIN', '$projectIN', '$ownerIN', '$regidIN')";
if (!($stmt = $mysqli->prepare( $queryone))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$stmt->close();
$mysqli->close();
?>
vars.php包含密码等的位置。您可能希望从用户在您的应用程序中完成的EdiTexts传递这些密码。由你决定。无论如何,我的vars.php看起来像:
<?php
$user="hidden";
$pwd="secret";
$database="also_secret";
?>
我遗漏了很多无关紧要的东西,但是您可以看到数据如何从Android端的参数,服务器上PHP的POST变量以及从那里到数据库的数据获取。您应该能够适应您的需求,祝您好运。