Pri*_*han 126 django android http
我想使用Http Post将图像从android客户端发送到Django服务器.图像从图库中选择.目前,我使用列表值名称Pairs将必要的数据发送到服务器并从JSON接收来自Django的响应.是否可以将相同的方法用于图像(使用嵌入在JSON响应中的图像的URL)?
此外,这是一种更好的方法:远程访问图像而无需从服务器下载图像或下载并将其存储在Bitmap数组中并在本地使用它们?图像数量很少(<10)且尺寸较小(50*50倾角).
任何解决这些问题的教程都将非常感激.
编辑:从库中选择的图像在将其缩放到所需大小后发送到服务器.
Pir*_*iro 143
我将假设您知道要上载的图像的路径和文件名.将此字符串添加到您的NameValuePair使用中image作为键名.
可以使用HttpComponents库完成发送图像.下载最新的HttpClient(目前4.0.1与依赖包)二进制和复制apache-mime4j-0.6.jar,并httpmime-4.0.1.jar到您的项目,并将它们添加到您的Java构建路径.
您需要将以下导入添加到您的类中.
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
Run Code Online (Sandbox Code Playgroud)
现在,您可以创建一个MultipartEntity将图像附加到POST请求.以下代码显示了如何执行此操作的示例:
public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
// If the key equals to "image", we use FileBody to transfer the data
entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这能帮助你朝着正确的方向前进.
AZ_*_*AZ_ 14
版本4.3.5更新的代码
自从MultipartEntity被弃用以来.请参阅下面的代码.
String responseBody = "failure";
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
String url = WWPApi.URL_USERS;
Map<String, String> map = new HashMap<String, String>();
map.put("user_id", String.valueOf(userId));
map.put("action", "update");
url = addQueryParams(map, url);
HttpPost post = new HttpPost(url);
post.addHeader("Accept", "application/json");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(MIME.UTF8_CHARSET);
if (career != null)
builder.addTextBody("career", career, ContentType.create("text/plain", MIME.UTF8_CHARSET));
if (gender != null)
builder.addTextBody("gender", gender, ContentType.create("text/plain", MIME.UTF8_CHARSET));
if (username != null)
builder.addTextBody("username", username, ContentType.create("text/plain", MIME.UTF8_CHARSET));
if (email != null)
builder.addTextBody("email", email, ContentType.create("text/plain", MIME.UTF8_CHARSET));
if (password != null)
builder.addTextBody("password", password, ContentType.create("text/plain", MIME.UTF8_CHARSET));
if (country != null)
builder.addTextBody("country", country, ContentType.create("text/plain", MIME.UTF8_CHARSET));
if (file != null)
builder.addBinaryBody("Filedata", file, ContentType.MULTIPART_FORM_DATA, file.getName());
post.setEntity(builder.build());
try {
responseBody = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8");
// System.out.println("Response from Server ==> " + responseBody);
JSONObject object = new JSONObject(responseBody);
Boolean success = object.optBoolean("success");
String message = object.optString("error");
if (!success) {
responseBody = message;
} else {
responseBody = "success";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
Run Code Online (Sandbox Code Playgroud)
von*_*ox7 10
该循环J库可以用于此目的的简单:
SyncHttpClient client = new SyncHttpClient();
RequestParams params = new RequestParams();
params.put("text", "some string");
params.put("image", new File(imagePath));
client.post("http://example.com", params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
// error handling
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
// success
}
});
Run Code Online (Sandbox Code Playgroud)
我努力实现使用httpclient-4.3.5.jar,httpcore-4.3.2.jar,httpmime-4.3.5.jar实现从Android客户端向servlet发布图像.我总是遇到运行时错误.我发现基本上你不能在Android上使用这些罐子,因为谷歌在Android中使用旧版本的HttpClient.这里的解释是http://hc.apache.org/httpcomponents-client-4.3.x/android-port.html.你需要从android http-client库获取httpclientandroidlib-1.2.1 jar .然后将导入从or.apache.http.client更改为ch.boye.httpclientandroidlib.希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
140634 次 |
| 最近记录: |