0
인터넷 (2G 네트워크 이상) 연결이 가능할 때마다 이미지, 텍스트를 서버로 보내려는 응용 프로그램을 만들고 있습니다.
1 분 후에 인터넷 연결을 확인하는 서비스의 타이머 스레드로이 논리를 구현하고 있습니다.데이터를 전송하는 동안 인터넷 연결을 확인하는 옵션을 찾으십시오.
여기이 논리에 문제가 있습니다.
a. 이미지를 전송하려면 1-2 분 (2G 이상)이 걸립니다. 그래서 사용자가 자신의 이미지를 업로드 할 때 한 지역 (인터넷 연결이 가능한 곳)에서 다른 지역 (인터넷 연결이 불가능한 곳)으로 이동할 경우이 경우 업로드 후 이미지를 얻을 수 없습니다.
그래서 위의 경우 이미지가 서버에 업로드되었는지 확인하십시오. 연결이 예외 (IOException이, TimeoutException 등)이 발생합니다 실패하면
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
덕분에, 나는 이미 예외를 잡은했다 모모 시도 {} catch 블록 월에 둘러싸인 연결을 넣어 나는 그것을 따로 잡을 필요가있다? –
코드를 게시 할 수 있습니까? – momo
, 내 코드, image1, image2, image3을 업로드하려고합니다. –