我的应用程序与wifi工作正常,但2g,3g的例外

时间:2017-01-16 10:19:30

标签: android file-upload ftp

主要发生在我将图像上传到服务器时。我正在使用ftp文件上传来上传images.i已经尝试过使用wifi,它的工作非常好。我是这个senario的新手。请帮助.....

我的文件上传课程:

 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();
            }
        }
    }

1 个答案:

答案 0 :(得分: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);