Hi All,

I want to send an image file as a part of the JSON request string to the
server.

String requestString =
"{\"meetcha\":{\"user_id\":\"19\",\"title\":\"volleyball at the sand
beach\",\"location_name\":\"BMO Bank of Montreal\","+
          "\"address1\":\"640 Bloor St.
W\",\"city\":\"Toronto\",\"state\":\"ON\",\"zip_code\":\"M6G1K9\","+
          "\"pictures_attributes\":{\"0\" :
{\"file\":"+fileName+",\"user_id\":\"19\"
} }  }  }";



In the above string, fileName is an image file.

But I am getting 500 as response code
If I remove the picture_attributes string, it will work fine.
But I want to send the images in the webservice.
I am pasting the code below:


 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

*try* {

DefaultHttpClient httpclient =

*new* DefaultHttpClient();

 // Working code

/* String *str* = "{\"*meetcha*\":{\"user_id\":\"19\","+

"\"title\":\"volleyball at the sand beach\",\"location_name\":\"BMO Bank of
*Montreal*\","+

"\"address1\":\"640 *Bloor* *St*. W\",\"city\":\"*Toronto*
\",\"state\":\"ON\",\"zip_code\":\"M6G1K9\","+

"\"description\":\"fun
*meetcha*\",\"start_date\":\"2011-11-04\",\"start_time\":\"21:25\","
+

"\"end_date\":\"2011-11-19\",\"end_time\":\"15:25\",\"skill_level\":\"\",\"required_item\":\"\","
+

"\"min_age\":\"\",\"max_age\":\"\",\"min_guest_num\":\"\",\"max_guest_num\":\"\"}
}";

 */

String image =

"mnt/sdcard/cartoon.gif";

File file =

*new* File(image);

  String requestString =

"{\"meetcha\":{\"user_id\":\"19\","+

"\"title\":\"volleyball at the sand beach\",\"location_name\":\"BMO Bank of
Montreal\","+

"\"address1\":\"640 Bloor St.
W\",\"city\":\"Toronto\",\"state\":\"ON\",\"zip_code\":\"M6G1K9\","+

"\"description\":\"fun
meetcha\",\"start_date\":\"2011-11-04\",\"start_time\":\"21:25\"," +

"\"end_date\":\"2011-11-19\",\"end_time\":\"15:25\",\"skill_level\":\"\",\"required_item\":\"\","+

"\"min_age\":\"\",\"max_age\":\"\",\"min_guest_num\":\"\",\"max_guest_num\":\"\",
" +

"\"pictures_attributes\":{\"0\" : {\"file\":"+file+",\"user_id\":\"19\" }
}},\"interests\":[151,23,7] }";

  System.

*out*.println("Request "+requestString);

 StringEntity se =

*new* StringEntity(requestString);

 HttpPost httppost =

*new* HttpPost("http://stg.meetcha.com/meetchas.json";);

 httppost.setHeader(

"Content-Type", "application/json");

httppost.setHeader(

"Accept", "application/json");

httppost.setHeader(

"USER_ACCESS_TOKEN", "3bd8ab45e2062ac39afd");

httppost.setEntity(se);

 HttpResponse response = httpclient.execute(httppost);

 *int* responseCode = response.getStatusLine().getStatusCode();

System.

*out*.println("Code:"+responseCode);

 InputStream is = response.getEntity().getContent();

 BufferedInputStream bis =

*new* BufferedInputStream(is);

ByteArrayBuffer baf =

*new* ByteArrayBuffer(20);

 *int* current = 0;

*while*((current = bis.read()) != -1){

baf.append((

*byte*)current);

}

 /* Convert the Bytes read to a String. */

String text1 =

*new* String(baf.toByteArray());

System.

*out*.println("response"+text1);

is.close();

bis.close();

  }

*catch* (Exception e) {

e.printStackTrace();

}







-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Regards,

Shruthi.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to