我正在尝试使用multipart方法将图像文件上传到服务器。
这是我的代码。
package com.example.test_multipart;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
TestAsyncTask asyncTask = new TestAsyncTask();
@Override
public void onClick(android.view.View v) {
// TODO Auto-generated method stub
new TestAsyncTask().execute();
}
});
}
public class TestAsyncTask extends AsyncTask<HttpResponse, Integer, Long> {
private ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
long totalSize;
String url = "http://bridgecall.co.kr:5000/apis/settings/profile_update";
@Override
protected Long doInBackground(HttpResponse... arg0) {
// Register parameters
Map<String, Object> params = new HashMap<String, Object>();
params.put("phone", "01030195208");
params.put("name", "YUJIHYN");
Map<String, File> fileParams = new HashMap<String, File>();
File f = new File("/DCIM/Camera/test.jpg");
fileParams.put("file", f);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Connection", "Keep-Alive");
httpPost.setHeader("Accept-Charset", "UTF-8");
httpPost.setHeader("ENCTYPE", "multipart/form-data");
CustomMultipartEntity multipart = new CustomMultipartEntity(
new ProgressListener() {
@Override
public void transferred(long transferred) {
// TODO Auto-generated method stub
publishProgress((int) transferred);
}
});
// Params
for (String strKey : params.keySet()) {
StringBody body = new StringBody(params.get(strKey)
.toString());
multipart.addPart(strKey, body);
}
// attach a file
for (String keys : fileParams.keySet()) {
multipart.addPart(keys, new FileBody(fileParams.get(keys)));
}
totalSize = multipart.getContentLength();
mDialog.setMax((int) totalSize);
httpPost.setEntity(multipart);
HttpResponse response = httpClient.execute(httpPost);
Log.v("checkcheckcheckcheck ", "checkcheckcheckcheck");
HttpEntity entity=response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
} catch (Exception e) {
return 0L;
}
return 0L;
}
@Override
protected void onCancelled() {
super.onCancelled();
mDialog.dismiss();
}
@Override
protected void onPostExecute(Long result) { // ui.
super.onPostExecute(result);
mDialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setCancelable(true);
mDialog.setOnCancelListener(cancelListener);
mDialog.setMessage("Uploading...");
mDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// Progress update
mDialog.setProgress((int) progress[0]);
}
OnCancelListener cancelListener = new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
}
};
}
}
此行之后无效。
Log.v("checkcheckcheckcheck ", "checkcheckcheckcheck");
我从服务器收到400错误。
如果我在HTML上传图像,它可以正常工作。你能看出问题是什么吗?