从android上传图片到PHP服务器

时间:2022-08-28 09:31:50

I'm trying to upload an image from android to a PHP server by using MultiPartEntity but i have trouble to find the source of the problem , the progress dialog steel downloading without providing any response here my code :

我正在尝试通过MultiPartEntity接口将一个图像从android上传到PHP服务器,但是我很难找到问题的根源,进度对话框steel下载时没有提供任何响应我的代码:

    public String postFunction(String s_v1, String s_v2, String s_v3)
throws ParseException, ClientProtocolException, IOException {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(s_v1);

//required apache-mime4j-0.6, et httpmime-4.0.3
MultipartEntity  mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

mp.addPart("choix", new StringBody("2"));
boolean exists = (new File("/data/data/my.package/files/avatar.jpg")).exists();  
if (!exists){

    Log.i(TAG,"no file");

} 
else
{
File tempImg = new File("/data/data/my.package/files/avatar.jpg");
FileBody bin = new FileBody(tempImg, "image/jpg");
mp.addPart("photo_r", bin);
}
mp.addPart("myString1", new StringBody(s_v2,  Charset.forName( "UTF-8" )));
mp.addPart("myString2", new StringBody(s_v3, Charset.forName( "UTF-8" )));

httppost.setEntity(mp);
Log.i(TAG,"start ");
HttpResponse response = httpclient.execute(httppost);
Log.i(TAG,"end");
return response;
}

i don't know where's the problem !! many thanks befor ;)

我不知道哪里出了问题!非常感谢之前,)

1 个解决方案

#1


1  

problem solved, it was because of my low internet connection, now I try to limit the connection time with SocketTimeoutException that is generated in the emulator, but unfortunately not in my phone acer liquid!

问题解决了,因为我的网络连接很低,现在我尝试用SocketTimeoutException来限制连接时间,这个异常是在模拟器中生成的,但不幸的是在我的手机宏基液体中没有!

#1


1  

problem solved, it was because of my low internet connection, now I try to limit the connection time with SocketTimeoutException that is generated in the emulator, but unfortunately not in my phone acer liquid!

问题解决了,因为我的网络连接很低,现在我尝试用SocketTimeoutException来限制连接时间,这个异常是在模拟器中生成的,但不幸的是在我的手机宏基液体中没有!