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