我正在尝试在android中编写一个程序,将文件上传到服务器。我想使用apache httpclient。
这是我的代码:
String url = "http://192.168.1.103:8080";
File file = new File("/sdcard/alireza/ali.txt");
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);
Toast.makeText(getApplicationContext(),"executing...",Toast.LENGTH_LONG).show();
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_LONG).show();
//Do something with response...
}catch (Exception e){
}
完成吐司并没有出现。