我在 android 中创建了一个表单活动,其中包含一些文本字段和照片,我想将此参数发送到服务器
我可以成功上传所有其他参数,但是图像没有上传到服务器,请帮我救救我..谢谢,我的代码如下:
Bitmap bitmap;
onClick(){
editEnable();
System.out.println("::::::::::save clicked:::::::::");
header.edit.setVisibility(View.GONE);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// new EditProfileAPI().execute();
new ImageUploadTask().execute();
// Profile Edit Call...!!!
break;
}
class ImageUploadTask extends AsyncTask<Void, Void, String> {
private StringBuilder s;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(HomeActivity.this);
pDialog.setMessage("Loading");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(Void... unsued) {
try {
String sResponse = "";
String url = Const.API_eDIT_PROFILE + "?";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("customer_id", new StringBody(Pref.getValue(HomeActivity.this, Const.PREF_CUSTOMER_ID, "")));
entity.addPart("first_name", new StringBody(et_firstname.getText().toString().trim()));
entity.addPart("last_name", new StringBody(et_lastname.getText().toString().trim()));
entity.addPart("customer_add", new StringBody(tv_adres.getText().toString().trim()));
entity.addPart("customer_phone", new StringBody(tv_phone.getText().toString().trim()));
entity.addPart("business_info", new StringBody(tv_busines.getText().toString().trim()));
entity.addPart("business_type", new StringBody(tv_busines_typ.getText().toString().trim()));
entity.addPart("bank_ac", new StringBody(tv_bank_acnt.getText().toString().trim()));
entity.addPart("dr_cr_card", new StringBody(tv_card.getText().toString().trim()));
entity.addPart("purpose_code", new StringBody(et_purpose_code.getText().toString().trim()));
entity.addPart("paypal_email", new StringBody(tv_paypal_email.getText().toString().trim()));
entity.addPart("password", new StringBody(et_fpassword.getText().toString().trim()));
entity.addPart("filename", new StringBody("test2.jpg"));
entity.addPart("files[]", new ByteArrayBody(data, "image/jpeg", "test2.jpg"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return s.toString();
} else {
return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println("::::::::::::::::::::::::::::MY exception in edit::::::::::::::::" + e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(String sResponse) {
try {
pDialog.dismiss();
if (sResponse != null) {
Toast.makeText(getApplicationContext(), sResponse + " Photo uploaded successfully", Toast.LENGTH_SHORT).show();
System.out.println("::::::::::::::::::::::::::::MY SUCCESS RESPONESE in edit::::::::::::::::" + sResponse);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}