この解決策を試してください。ほとんどの種類のファイルで機能します。
private DefaultHttpClient mHttpClient;
Context context;
public String error = "";
//Contrutor para que metodos possam ser usados fora de uma activity
public HTTPconector(Context context) {
this.context = context;
}
public HTTPconector() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mHttpClient = new DefaultHttpClient(params);
}
public void FileClientPost(String txtUrl, File file){
try
{
error = "";
HttpPost httppost = new HttpPost(txtUrl);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Image", new FileBody(file));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
}
catch (Exception e)
{
Log.e(HTTPconector.class.getName(), e.getLocalizedMessage(), e);
e.getStackTrace();
error = e.getMessage();
}
}
//Verifica se a rede esta disponível
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
public String Get(String txtUrl){
try {
URL url = new URL(txtUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
return readStream(con.getInputStream());
} catch (ProtocolException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
}
}
public String Post(String txtUrl){
File image;
try {
URL url = new URL(txtUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
//con.getOutputStream().write( ("name=" + "aa").getBytes());
return readStream(con.getInputStream());
} catch (ProtocolException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
}
}
//Usado para fazer conexão com a internet
public String conectar(String u){
String resultServer = "";
try {
URL url = new URL(u);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
resultServer = readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
resultServer = "ERRO: "+ e.getMessage();
}
Log.i("HTTPMANAGER: ", resultServer);
return resultServer;
}
//Lê o resultado da conexão
private String readStream(InputStream in) {
String serverResult = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
serverResult = reader.toString();
} catch (IOException e) {
e.printStackTrace();
serverResult = "ERRO: "+ e.getMessage();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
serverResult = "ERRO: "+ e.getMessage();
}
}
}
return serverResult;
}
private class PhotoUploadResponseHandler implements ResponseHandler<Object>
{
@Override
public Object handleResponse(HttpResponse response)throws ClientProtocolException, IOException {
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
Log.d("UPLOAD", responseString);
return null;
}
}