Reputation: 25
In my project,I need to transfer image and string to server(server side uses php).I completed uploading images to server.So the only question is how can I send string to server.Can anyone tell me how to do it?
Upvotes: 0
Views: 441
Reputation: 2548
Here is some code that should point you in the correct direction.
First, use something like this on your application side:
Java:
// generate your params:
String yourString = "This is the string you want to send";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("your_string", yourString));
// send them on their way
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://xyz/your_php_script.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValueParams));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
And pick it up with something like this on your server side (http://xyz/your_php_script.php):
PHP:
<?php
if (isset($_POST['your_string']) && $_POST['your_string'] != '') {
$your_string = $_POST['your_string'];
echo 'received the string: ' . $your_string;
} else {
echo 'empty';
}
?>
Edit, per your comment:
It is more complicated because you have to use a OutputStream
and BufferedWriter
, so I don't know why my solution won't work for you. Using Google, I found the following answers that may help you:
Upvotes: 1