duan19911992 2013-07-25 09:06
浏览 282

上传图片 - 发送请求到PHP脚本 - Android

I would like to upload an image by sending the image to a php script. It works fine if the internet connection is good. But sometimes it does not work. E.g. when the internet connection is slow. So I'm looking for a way to upload the image with a slow connection. What can I do? The filesize is already quite small, about 40kb. The code should try to upload the image until it is done.

Edit: Code for the upload:

public void upload(String uploadImage){
     SharedPreferences settings = getSharedPreferences("App", 0);
     String emailaddress = settings.getString("mailaddress", null);
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("image",uploadImage));
     nameValuePairs.add(new BasicNameValuePair("email", emailaddress));
     try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new
            HttpPost("http://domain.de/uploadImage.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
     }catch(Exception e){
            try {
            Thread.sleep( 10000 );
            }catch (InterruptedException e1) {
            e1.printStackTrace();
            }
            upload(uploadImage);
     }
}
  • 写回答

1条回答 默认 最新

  • duangu9666 2013-07-25 10:00
    关注

    Use AsyncTask to run your upload process in background.. you can show your progressbar while loading the operation.

    public class UploadProgress extends AsyncTask<Void, Void, Void>
    {  
    @Override
    protected void onPreExecute()
    {  
    super.onPreExecute();
    dialog.setMessage("Please wait...");
    dialog.setCancelable(false);
    dialog.show(); 
    } 
    @Override 
    protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
    // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
    }
    
    @Override 
    protected void onPostExecute(Void result) 
    { 
      //Post Execute
       dialog.cancel();
    }
    @Override
    protected Void doInBackground(Void... params) {
    
    // Your operation..Dont Edit Any Views here
    boolean isDataSubmitted = false;
            while (!isDataSubmitted) {
                count++;
                if (count < 2)//Count depends on you {
                    try {
                       //Call your upload  method here 
                       //If its success your dataSubmitted will go true
    
                       isDataSubmitted = true;
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
    
    return null;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口