[英]how to Post parmeter in Json format to server for Uploading Image
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;
}
}
}
这是我的将图像上传到服务器的代码,我可以将参数以名称值对[member_id = 380,imageFile = bytestream,picturepathe =“ 380.jpg”]发送到服务器。 而我必须像这样发布参数{[imageFile:“ bystram”,member_id:“ 380”,picture_path:“ 380.jpg”]}
请帮助我将如何以Json格式制作名称值对
您可以这样在json中发布数据:-
String json = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id", "");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
jsonObject.put("MSISDN", number);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = null;
您可以发布json对象,如下所示
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);
try {
String finalnamevalue;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://api.lociiapp.com/api/registration/SaveProfilePicture");
JSONObject contactsObj = new JSONObject();
contactsObj.put("member_id", "380");
contactsObj.put("imageFile", image_str);
contactsObj.put("picture_path", "380.jpg");
StringEntity stringEntity = new StringEntity( contactsObj.toString());
stringEntity.setContentType("application/json;charset=UTF-8");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
httppost.setEntity(stringEntity);
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;
}
更改帖子标题和内容类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.