0

I am creating a application in which I want to send image,text to server whenever internet(over 2G network) connection is available.
I am implementing this logic with timer thread in service which checks internet connection is available or not after 1 minute.

Here I am getting problem with this logic :
a. For sending image to it will take time 1-2 minute (over 2G). So in case when user upload his image and he move from one area(where internet connection is available) to other area (where internet connection is not available) So in this case I am not able to get image after uploading .

So How ensure Image is uploaded to server or not in above case.

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

1 Answers1

0

If the connection fails there will be an exception thrown (IOException, TimeoutException, etc), just put the connection surrounded by a try {} catch block

momo
  • 3,404
  • 6
  • 37
  • 66
  • thanks momo,I had caught Exception already,May I need to caught it separately ?? – Akshay Bhongale Oct 13 '14 at 08:06
  • Can you post your code? It will be easier to know what is going wrong :-) – momo Oct 13 '14 at 08:11
  • ,I am uploading my code,image1,image2,image3 is nothing but path of image – Akshay Bhongale Oct 13 '14 at 10:31
  • The code looks fine to me, you should be able to detect the Exception and that would mean the photos upload failed (In that case you should retry the connection automatically or show an error and let the user click to upload again). Sorry I can't be of more help, maybe others will – momo Oct 14 '14 at 08:18