【问题标题】:my app works fine with wifi but gives exception on 2g ,3g我的应用程序在 wifi 上运行良好,但在 2g ,3g 上出现异常
【发布时间】:2017-01-16 10:19:29
【问题描述】:

这主要发生在我将图像上传到服务器时。我正在使用 ftp 文件上传器上传图像。我已经尝试使用 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();
            }
        }
    }

【问题讨论】:

  • 将 logcat 添加到您的查询中,您遇到了什么异常!
  • @SadiqMdAsif ...我找不到它。当我使用像沃达丰这样的不同运营商时,它有时会起作用。严重的问题是 jio 4g
  • 我已经敬酒来处理它,就像..检查你的互联网连接
  • 但它应该可以工作,因为其他应用程序也可以正常工作
  • 看看下面的Android Studio有一个选项Android Monitor点击它就可以得到logcat

标签: android file-upload ftp


【解决方案1】:

您的 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);

【讨论】:

  • 同样...握手超时
  • 我已经根据你的建议编辑了这个问题。请看看并告诉我那里是否有问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多