Thursday, April 16, 2015

Syncing image/audio/video with parameters to the server in android

In any applications if you are working with a client server application, there is a high probability of syncing the images with some parameters to the server.
We have seen lot of examples to sync the text or parameters to the server and also the image or audio or video or any files to the server. You see these examples separately to send these data.  But not the combined data to the server.

Now, in this blog we are going to learn how we are going to send the the image and some parameters together to the server. Here the client is an android application and the server is apache with php and mysql (database).

We can consider the example of a register screen in the android application. Here I am not going to talk about the server set up, PHP and mysql configuration. It is assumed that the server setup and the PHP/Mysql configuration is done, up and running. Data base related to the registration parameters also ready. I am going to consider the First Name,  Last Name, Address, Mobile Number, Email, Gender and image of the candidate to be registered.

These data is captured from the mobile and sent to the server via PHP web service.  Here I am discuss about the code written from the client side. I am going to write that in a method "postdata"  It is assumed that we know the web service URL of the server to send the data.
define:
public final String Web_URL = "http://xxxx.com/xxxx_reg.php";

@Override
public string postdata() {
// Create a new HttpClient and Post Header
try {
 HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Web_URL);
        HttpContext localContext = new BasicHttpContext();
/* Create a object for Mutipart Entity to send the data together
MultipartEntity multipartEntity = new MultipartEntity();
String path ="image_path"; //user has to enter his image path where the captured image is stored
File sourceFile = new File(path);
FileBody bin1 = new FileBody(sourceFile);
multipartEntity.addPart("uploaded_file", bin1);
multipartEntity.addPart("fName", "Jose");
multipartEntity.addPart("lName", "Pareria");
multipartEntity.addPart("address","#999, 2nd Main, 10th Block, Koramangala, Bangalore-10");
multipartEntity.addPart("mobile", "+919999999999");
multipartEntity.addPart("email", "abc@xyz.com");
multipartEntity.addPart("gender", "Male");
//Setting the Entity and executing
httppost.setEntity(multipartEntity);
HttpResponse response = httpclient.execute(httppost,localContext);
// Getting the Respose from the server
                  BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
String getData = builder.toString();
System.out.println(" data "+getData);
}catch (ClientProtocolException e) {
                 Log.w("Network Error",e.toString());
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
Log.w("IO Exception",e.toString());
}
return "";
}
That's it. Try it out. Happy coding.

No comments: