Android 正在查找在发送数据时检查internet连接的选项

Android 正在查找在发送数据时检查internet连接的选项,android,image,http-post,multipartentity,Android,Image,Http Post,Multipartentity,我正在创建一个应用程序,在这个应用程序中,每当internetover 2G网络连接可用时,我都要向服务器发送图像和文本。 我正在使用服务中的计时器线程实现此逻辑,该线程在1分钟后检查internet连接是否可用 在这里,我对这种逻辑产生了疑问: A.向其发送图像需要1-2分钟(2G)。所以,当用户上传他的图像时,他从一个有互联网连接的区域移动到另一个没有互联网连接的区域,所以在这种情况下,上传后我无法获得图像 在上述情况下,如何确保图像上传到服务器上 public String uploadF

我正在创建一个应用程序,在这个应用程序中,每当internetover 2G网络连接可用时,我都要向服务器发送图像和文本。 我正在使用服务中的计时器线程实现此逻辑,该线程在1分钟后检查internet连接是否可用

在这里,我对这种逻辑产生了疑问: A.向其发送图像需要1-2分钟(2G)。所以,当用户上传他的图像时,他从一个有互联网连接的区域移动到另一个没有互联网连接的区域,所以在这种情况下,上传后我无法获得图像

在上述情况下,如何确保图像上传到服务器上

public String uploadFileToServer(String image1,String image2,String image3) {

    String result = null;
    try {
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 25000);
            HttpConnectionParams.setSoTimeout(httpParameters, 25000);
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postResquest= new HttpPost(URL);
            httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            // code for send image using post method
            System.out.println("Image1="+image1);
            System.out.println("Image2="+image2);
            System.out.println("Image3="+image3);
            reqEntity.addPart("image1",new FileBody(new File(image1)));
            reqEntity.addPart("image2",new FileBody(new File(image2)));
            reqEntity.addPart("image3",new FileBody(new File(image3)));

            System.out.println("uploaded"+"image added Parameter added");
            postResquest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postResquest);
            int sucess=response.getStatusLine().getStatusCode();
            System.out.println("status code:"+sucess);
            if(sucess==200)
            {   
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) 
                {
                    s = s.append(sResponse);
                }
                System.out.println("Upload photo Response" + s);
                result=s.toString();
            }
            else
            {
                  HashMap<String, String> hashMap=new HashMap<String, String>();
                  hashMap.put("flag", "-1");

                  JSONObject jsonObject =new JSONObject(hashMap);
                  result=jsonObject.toString();
            }
          // return getUploadResponce(s.toString());
          // Log.i("Response ", );
    } 
    catch (Exception e) 
    {
          // TODO Auto-generated catch block
          e.printStackTrace();

          HashMap<String, String> hashMap=new HashMap<String, String>();
          hashMap.put("flag", "-1");

          JSONObject jsonObject =new JSONObject(hashMap);
          result=jsonObject.toString();

      }
        return result;
} // end of upload file toserver

如果连接失败,将抛出异常IOException、TimeoutException等,只需将连接用try{}catch块包围即可

谢谢momo,我已经捕获了异常,我需要单独捕获吗?你能发布你的代码吗?这将更容易知道出了什么问题:-,我正在上传我的代码,image1,image2,image3只是image3的路径。代码在我看来很好,您应该能够检测到异常,这意味着照片上载失败。在这种情况下,您应该自动重试连接,或者显示错误,让用户单击以再次上载。对不起,我帮不上什么忙,也许其他人会的