2017-01-16 7 views
-1

Это происходит, главным образом, когда я загружаю изображения на сервер. Я использую ftp-uploder для загрузки изображений. Я попробовал его с Wi-Fi, работая абсолютно нормально. Я новичок в этом Senario .pls помочь .....мое приложение отлично работает с Wi-Fi, но дает исключение на 2g, 3g

Мой класс uploder Файл:

public void uploadFile(Context context, File fileName) { 
     FTPClient client = new FTPClient(); 

     try { 
      flag = false; 
      if (!client.isConnected()) { 
      client.setSecurity(FTPClient.SECURITY_FTPES); 
      // client.setSecurity(FTPClient.SECURITY_FTP); 
       try { 
        client.connect(FTP_HOST, 21); 
        client.setType(FTPClient.TYPE_BINARY); 
        SSLContext sc = SSLContext.getInstance("SSL"); 
        sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
        client.setSSLSocketFactory(sc.getSocketFactory()); 
        client.login(FTP_USER, FTP_PASS); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       client.setType(FTPClient.TYPE_BINARY); 

       client.changeDirectory(UPLOAD_PATH); 
       client.upload(fileName); 
       flag = true; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      try { 
       client.disconnect(true); 
      } catch (Exception e2) { 
       e2.printStackTrace(); 
      } 
      flag = false; 
     } 
    } 

Мои Асинхронный задача:

private class SellAsync extends AsyncTask<String, String, String> 
    { 
     final ProgressDialog progressDialog = new ProgressDialog(Sell_Main.this); 
     long totalSize = 0; 

     @Override 


     protected String doInBackground(String... urls) 
     { 
      String msg = ""; 
      try 
      { 

        for (int i = 0; i < imageList.size(); i++) 
        { 
         File f = new File(imageList.get(i)); 
         new FileUploader().uploadFile(Sell_Main.this, f); 

         if (!FileUploader.flag) 
         { 
          throw new Exception("File uploading failed"); 
         } 
        } 


       ArrayList<NameValuePair> arr = new ArrayList<NameValuePair>(); 
       arr.add(new BasicNameValuePair("brand", brand)); 
       arr.add(new BasicNameValuePair("model", model)); 
       arr.add(new BasicNameValuePair("variant", variant)); 
       arr.add(new BasicNameValuePair("fuel_type", fuelspinner)); 
       arr.add(new BasicNameValuePair("kilometres", km)); 
       arr.add(new BasicNameValuePair("transmission", trans)); 
       arr.add(new BasicNameValuePair("make_year", makeyear)); 
       arr.add(new BasicNameValuePair("Expected_price", price)); 
       arr.add(new BasicNameValuePair("ownership", owner)); 
       arr.add(new BasicNameValuePair("location", location)); 
       arr.add(new BasicNameValuePair("cid", sPref.getString("id", ""))); 

       String imagesList = ""; 
       for (int i = 0; i < imageList.size(); i++) { 
        File f = new File(imageList.get(i)); 
        imagesList += f.getName() + ","; 
       } 
       arr.add(new BasicNameValuePair("images", imagesList.substring(0, imagesList.length() - 1))); 

       HttpParams httpParameters = new BasicHttpParams(); 
// Set the timeout in milliseconds until a connection is established. 
// The default value is zero, that means the timeout is not used. 
       int timeoutConnection = 15000; 
       HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data. 
       int timeoutSocket = 30000; 
       HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 


       HttpClient client = new DefaultHttpClient(httpParameters); 

       HttpPost post = new HttpPost(urls[0]); 

       post.setEntity(new UrlEncodedFormEntity(arr)); 

       HttpResponse response = client.execute(post); 

       msg = EntityUtils.toString(response.getEntity()); 

      } catch (Exception e) { 
       msg = e.getMessage(); 
      } 
      return msg; 
     } 



     @Override 
     protected void onPreExecute() 
     { 
      progressDialog.setMessage("Uploading...it may take some time."); 
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      progressDialog.show(); 
      progressDialog.setCancelable(false); 
      progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 
       @Override 
       public void onDismiss(DialogInterface dialog) { 
        cancel(true); 
       } 
      }); 
      super.onPreExecute(); 
     } 

     @Override 
     protected void onPostExecute(String s) 
     { 
      super.onPostExecute(s); 
      progressDialog.dismiss(); 
      if (s.equalsIgnoreCase("inserted successfully")) 
      { 
       Toast.makeText(getApplicationContext(), "Details Submitted successfully,will revert you back soon..", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
      else if(s.equalsIgnoreCase("File uploading failed")) 
      { 
       Toast.makeText(getApplicationContext(), "Please check your internet connection", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
+0

добавить к вашему запросу логарифм, в какое исключение вы попадаете! –

+0

@SadiqMdAsif ... я не могу его найти. Когда я использую различные носители, такие как vodafone, это работает иногда. Особые проблемы с jio 4g –

+0

Я дал тост, чтобы справиться с ним, как ..check ur интернет-соединение –

ответ

0

Ваше соединение WiFi имеет хорошую скорость, но 3g/2g этого не имеет. Таким образом, это время. увеличить время ожидания подключения

HttpParams httpParameters = new BasicHttpParams(); 
// Set the timeout in milliseconds until a connection is established. 
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data. 
int timeoutSocket = 5000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

HttpClient httpClient = new DefaultHttpClient(httpParameters); 
HttpPost post = new HttpPost(urls[0]); 

post.setEntity(new UrlEncodedFormEntity(arr)); 
HttpResponse response = httpClient.execute(post); 
+0

его не работает, даже если я увеличиваю тайм-аут –

+0

что показывает лог-каток сейчас? –

+0

такой же ... рукопожатие тайм-аут –