使用 Android HTTP 客户端将文件上传到 REST api 服务器
Uploading file to a REST api server using Android HTTP client
我正在使用名为 json-server 的虚拟 rest API 服务器。我写了一个 android 应用程序,它能够向服务器发出 POSt 请求,服务器 returns 也是预期的 201。
我对REST不是很熟悉api。我想从我的设备发送文件
public void run() {
Logger.d("reponse","inside thread");
HttpClient client = new DefaultHttpClient(TunnelView.this);
String url = "http://some_server:3000/comments";
HttpGet get = new HttpGet(url);
HttpPost post = new HttpPost(url);
HttpResponse response = null;
post.setEntity(new FileEntity(new File("/root/sdcard/Download/File.txt"),"application/octect-stream"));
try
{
response = client.execute(post);
String res = response.getEntity().getContent().toString();
Logger.d("response", res);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
每次我执行 post 时,都会在位于其余 API 服务器上的 json 文件的 Comments 数组下添加一个新 ID。
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
},
{
"id": 2
},]
我不确定如何更改我的 android 代码或 REST API Post URl 所以我可以发送整个文本文件内容并且它得到 posted 在服务器上的 json 文件上。
试试这个
public static JSONObject postFile(String url,String filePath,int id){
String result="";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
File file = new File(filePath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
StringBody stringBody= null;
JSONObject responseObject=null;
try {
stringBody = new StringBody(id+"");
mpEntity.addPart("file", cbFile);
mpEntity.addPart("id",stringBody);
httpPost.setEntity(mpEntity);
System.out.println("executing request " + httpPost.getRequestLine());
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
result=resEntity.toString();
responseObject=new JSONObject(result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return responseObject;
}
此代码完美运行,使用它,如果您发现任何困难,请发表评论。
Happy coding!!
我正在使用名为 json-server 的虚拟 rest API 服务器。我写了一个 android 应用程序,它能够向服务器发出 POSt 请求,服务器 returns 也是预期的 201。
我对REST不是很熟悉api。我想从我的设备发送文件
public void run() {
Logger.d("reponse","inside thread");
HttpClient client = new DefaultHttpClient(TunnelView.this);
String url = "http://some_server:3000/comments";
HttpGet get = new HttpGet(url);
HttpPost post = new HttpPost(url);
HttpResponse response = null;
post.setEntity(new FileEntity(new File("/root/sdcard/Download/File.txt"),"application/octect-stream"));
try
{
response = client.execute(post);
String res = response.getEntity().getContent().toString();
Logger.d("response", res);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
每次我执行 post 时,都会在位于其余 API 服务器上的 json 文件的 Comments 数组下添加一个新 ID。
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
},
{
"id": 2
},]
我不确定如何更改我的 android 代码或 REST API Post URl 所以我可以发送整个文本文件内容并且它得到 posted 在服务器上的 json 文件上。
试试这个
public static JSONObject postFile(String url,String filePath,int id){
String result="";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
File file = new File(filePath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
StringBody stringBody= null;
JSONObject responseObject=null;
try {
stringBody = new StringBody(id+"");
mpEntity.addPart("file", cbFile);
mpEntity.addPart("id",stringBody);
httpPost.setEntity(mpEntity);
System.out.println("executing request " + httpPost.getRequestLine());
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
result=resEntity.toString();
responseObject=new JSONObject(result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return responseObject;
}
此代码完美运行,使用它,如果您发现任何困难,请发表评论。
Happy coding!!