0

What I need: I need connect to my ftp server from android app because I want upload images to this server from my android app and also I want upload some data(string...) to mySQL DB on this server too.

The problem is: If I want connect to ftp I need in java code write my login and pass to access to my FTP or login to mySQL DB like in these examples> source 1 and source 2 but I dont want write my login, pass in java code becasue anyone can reverse my app and then he can edit my DB atc this is not good way.

The question is: How or what I can use to authentication?

What I am using: I'm using Apache server with php and mySQL db.

I will be grateful for any help.

Community
  • 1
  • 1
pavol.franek
  • 1,396
  • 3
  • 19
  • 42
  • Do you definitely want to do the transfer by FTP rather then HTTP under Apache? – NickT Feb 24 '14 at 13:46
  • Transfer by ftp is my idea I dont have any experience there. I welcome any another idea and way how I can upload data and images to my server. But as I say in comment bellow Im using hosting ftp server and I cannot edit etc apache server. I can only use DB and php to part control of server. I have standard hosting with domain. – pavol.franek Feb 24 '14 at 13:52
  • 1
    OK, I'll post a cutdown example using HTTP which is the Apache default protocol. Might take a little while – NickT Feb 24 '14 at 14:02

2 Answers2

1

You need to create a DataSource on your apache server.Most programming languages give support for using a DataSource to get a DataBase connection.In that way,your server admin controls the database connection details with the application.

You can find more info here:-

https://confluence.atlassian.com/display/DOC/Configuring+a+SQL+Server+Datasource+in+Apache+Tomcat

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • thx for TIP but problem is Im using hosting ftp server and I cannot edit etc apache server. I can only use DB and php to part control of server. I think there must be way only with php where I create some service which will do authentication maybe. – pavol.franek Feb 24 '14 at 12:44
1

I've got a raspberry pi running as a small server running Apache, PHP and a MySQL DB to hold the registrationIDs for GCM apps running on Android devices. When a device is registered it sends the regID to the DB.

The table is called registrations and it has columns device, project, owner, regid.

I should stress that the following code on the Android phone is all wrapped up in an AsyncTask and that it is invoked in the doInBackground method.

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();
        }
    }

}

On the server I have:

<?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();
?>

Where vars.php holds the passwords etc. These you might want to pass in from EdiTexts that the user completes in your application. It's up to you. Any way my vars.php looks like:

<?php
   $user="hidden";
   $pwd="secret";
   $database="also_secret";
?>

I've left out a fair bit of extraneous stuff but you can see how data gets from the params on the Android side, to the POST variables in the PHP on the server and from there into the database. You should be able to adapt it for your needs, good luck.

NickT
  • 23,844
  • 11
  • 78
  • 121