0

I want to save a message in a text file on an android phone onto a hosted web server like bluehost of which I have a username and password. I want to store the file in an arbitrary directory on the server.

What are the general strategies that this could be accomplished? I want to use the HTTP protocol, is that a good idea? Is there a better way?

antoniom
  • 3,143
  • 1
  • 37
  • 53
Sammar javed
  • 153
  • 1
  • 5
  • -1, what have you tried? how will you communicate with (what kind of) server? – thelawnmowerman Jan 15 '13 at 15:11
  • I am working on local host.I want to save that text file to I want to communicate throught Http client – Sammar javed Jan 15 '13 at 15:12
  • Yeah, but one major aspect is what is on the server itself. Is it an Apache server with PHP? If so, you can use the answer posted by antoniom to POST the string and write the file on the server. Or you can just upload the file. Google: "How to post file to PHP from Android" and see the code that already exists.. – burmat Jan 15 '13 at 15:15
  • i know this.But i want to write that string in a text file on android side and then want to send that text file to webserver – Sammar javed Jan 15 '13 at 15:15

2 Answers2

1

You can try to POST that string to the server:

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.example.com");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("yourVarName", stringVar);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        return (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 204);

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
antoniom
  • 3,143
  • 1
  • 37
  • 53
0

Transmit a file from an android phone to hosted web space

You import the JSCH jars into your Android application, then you load up the JSCH manager classes and use the defined functions to transmit or receive files between an android phone and hosted webspace.

Run a command over SSH with JSch

JSCH has FTP functionality where you can transmit from phone to hosted web space and will work as long as the hosted web space is reachable by the phone. You can also do the same thing in reverse.

Community
  • 1
  • 1
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335