如何在Android中发送图像或文本文件
实际上,我正在开发一个应用程序,它使用json作为名称-值对发送提交表单,用于服务器通信。但现在的问题是,我想在提交表单时发送图像或文本文件,在提交表单时如何发送图像或文本文件。有正确的程序吗?对于文件,请尝试以下代码:如何在Android中发送图像或文本文件,android,androidhttpclient,Android,Androidhttpclient,实际上,我正在开发一个应用程序,它使用json作为名称-值对发送提交表单,用于服务器通信。但现在的问题是,我想在提交表单时发送图像或文本文件,在提交表单时如何发送图像或文本文件。有正确的程序吗?对于文件,请尝试以下代码: String url = "http://yourserver"; File file = new File(Environment.getExternalStorageDirectory(), "yourfile"); try { HttpClient
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}
请尝试以下代码以将图像上载到服务器:
private class ImageUploader extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String result = "";
// Client-side HTTP transport library
HttpClient httpClient = new DefaultHttpClient();
// using POST method
HttpPost httpPostRequest = new HttpPost(imagePostUrl);
try {
// creating a file body consisting of the file that we want to
// send to the server
FileBody bin = new FileBody(imageFile);
/**
* An HTTP entity is the majority of an HTTP request or
* response, consisting of some of the headers and the body, if
* present. It seems to be the entire request or response
* without the request or status line (although only certain
* header fields are considered part of the entity).
*
* */
MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart("images[1]", bin);
httpPostRequest.setEntity(multiPartEntityBuilder.build());
// Execute POST request to the given URL
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPostRequest);
// receive response as inputStream
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
return result;
} catch (Exception e) {
return null;
}
// return result;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
uploadStatus.setText("Uploading image to server");
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
uploadStatus.setText(result);
}
}
private static String convertInputStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
私有类ImageUploader扩展异步任务{
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
字符串结果=”;
//客户端HTTP传输库
HttpClient HttpClient=新的DefaultHttpClient();
//使用POST方法
HttpPost httpPostRequest=新的HttpPost(imagePostUrl);
试一试{
//创建一个包含我们想要创建的文件的文件体
//发送到服务器
FileBody bin=新的FileBody(imageFile);
/**
*HTTP实体是HTTP请求或请求的主体
*响应,包括一些标题和正文(如果需要)
*现在。它似乎是整个请求或响应
*不带请求行或状态行(尽管只有确定的
*标题字段被视为实体的一部分)。
*
* */
MultipartEntityBuilder MultipartEntityBuilder=MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart(“图像[1]”,bin);
setEntity(multiPartEntityBuilder.build());
//执行对给定URL的POST请求
HttpResponse HttpResponse=null;
httpResponse=httpClient.execute(httpPostRequest);
//作为inputStream接收响应
InputStream InputStream=null;
inputStream=httpResponse.getEntity().getContent();
如果(inputStream!=null)
结果=convertInputStreamToString(inputStream);
其他的
结果=“不起作用!”;
返回结果;
}捕获(例外e){
返回null;
}
//返回结果;
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
setText(“将图像上载到服务器”);
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
uploadStatus.setText(结果);
}
}
私有静态字符串convertInputStreamToString(InputStream InputStream)
抛出IOException{
BufferedReader BufferedReader=新的BufferedReader(
新的InputStreamReader(inputStream));
字符串行=”;
字符串结果=”;
而((line=bufferedReader.readLine())!=null)
结果+=行;
inputStream.close();
返回结果;
}
ya您可以通过post方法发送图像和文本文件。但是这里很难解释。请检查这个@pratik MultipartEntityBuilder无法解析为类型。MultipartEntityBuilder是否可以在android 2.3.3中工作?@草莓您必须下载httpclient和HttpTime jar文件,并需要放入lib文件夹,然后需要添加到构建路径。之后,您将获得所有类的使用。