public class UploadImage extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new myclass().execute();
}
class myclass extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); // compress
// to
// which format
// you want.
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
String finalnamevalue;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://api.lociiapp.com/api/registration/SaveProfilePicture");
JSONObject contactsObj = new JSONObject();
nameValuePairs.add(new BasicNameValuePair("member_id", "380"));
nameValuePairs.add(new BasicNameValuePair("imageFile",
image_str));
nameValuePairs.add(new BasicNameValuePair("picture_path",
"380.jpg"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
} catch (Exception e) {
System.out.println("Error in http connection " + e.toString());
}
return null;
}
public String convertResponseToString(HttpResponse response)
throws IllegalStateException, IOException {
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); // getting
// content
// length…..
if (contentLength < 0) {
} else {
byte[] data = new byte[512];
int len = 0;
try {
while (-1 != (len = inputStream.read(data))) {
buffer.append(new String(data, 0, len)); // converting
// to
// string
// and
// appending
// to
// stringbuffer…..
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close(); // closing the stream…..
} catch (IOException e) {
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..
// System.out.println("Response => " +
// EntityUtils.toString(response.getEntity()));
}
return res;
}
}
}
This is My code for Upload image to server i am able to send parameter to server in name value pair [member_id=380, imageFile=bytestream,picturepathe="380.jpg"] . while i have to Post parameter like this {[imageFile:"bystram",member_id:"380",picture_path:"380.jpg"]}
please help how i will make Name value pair in Json format