I am creating an android application which allows user to submit some reviews on items. How can I send this review in HTTPRequest (POST method)? Also I would like to do some kind of en开发者_JAVA技巧coding before i send out any information. What are the encoding options available in android?
Any exmaple would be really helpful.
Rgds, Sapan
import android.util.Base64;
public class Test {
public static void main(String[] args) {
String data = "this is a message by frank.";
String encoded = Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
System.out.println("Encoded: " + encoded);
String decoded = new String(Base64.decode(encoded, Base64.DEFAULT));
System.out.println("Decoded: " + decoded);
}
}
The output is:
Encoded: dGhpcyBpcyBhIG1lc3NhZ2UgYnkgZnJhbmsu
Decoded: this is a message by frank.
Let’s try decode the message in python:
import base64
print base64.b64decode('dGhpcyBpcyBhIG1lc3NhZ2UgYnkgZnJhbmsu')
Using org.apache.http.client.HttpClient and org.apache.http.client.methods.HttpPost you can send/receive http req/resp in android. For more detail and example see Android - A Quick and Dirty Http Client
for decoding you can user base64
Just in case you meant marshaling into DOM, you might want to use JSON. This class provides a bunch of methods for easily creating json trees for sending the data to a server. JSON is usually a bit faster and requires less characters than XML.
精彩评论