Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将文件上载到http_Android - Fatal编程技术网

Android 将文件上载到http

Android 将文件上载到http,android,Android,我正在将.rar上载到我的http服务器。。。如果SD卡上没有文件,如何取消上载。。。这是我的密码 private class SessionTask extends AsyncTask<String, Integer, Integer> { ProgressDialog dialog; @Override protected void onPreExecute() { super.onPreExecute(); Wifi

我正在将.rar上载到我的http服务器。。。如果SD卡上没有文件,如何取消上载。。。这是我的密码

 private class SessionTask extends AsyncTask<String, Integer, Integer> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
        Vibrator v2 = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        v2.vibrate(1500);
        dialog = new ProgressDialog(TestUI.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle("UploadFile");
        dialog.setMessage("Uploading file...");
        dialog.setCancelable(false);
        dialog.setProgress(0);
        dialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {

            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            MultipartEntity multipart = new MultipartEntity();
            File file = new File("/mnt/sdcard/321.rar");  // File with some location (filepath)
            Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
            FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
            multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file

            StringBody stringB;  // Now lets add some extra information in a StringBody
            try {
                stringB = new StringBody("I am the caption of the file",chars);  // Adding the content to the StringBody and setting up the encoding
                multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            HttpPost post = new HttpPost("http://192.168.1.1:9999/folder/0"); // Setting up a HTTP Post method with the target url
            post.setEntity(multipart); // Setting the multipart Entity to the post method
            HttpResponse resp = httpclient.execute(post);  // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
        } catch(MalformedURLException e) {
            Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());

            return 1;
        } catch(IOException e) {
            Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
        return 2;
        }
        return 0;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialog.setMax(values[1]);
        dialog.setProgress(values[0]);

    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        dialog.dismiss();
        switch (result) {
        case 0:
            Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
            new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
            break;
        case 1:
            Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
            break;
        case 2:
            Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
            try {
                Thread.sleep(timesleep);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            new UploadTask().execute();
            break;
        default:
            break;
        }
    }
} 
当没有要上载的文件时-会发生此类错误。。。 01-05 22:12:08.887:D/dalvikvm6626:GC_FOR_MALLOC在108ms内释放了1334个对象/250744个字节 01-05 22:12:13.230:D/dalvikvm6626:GC_FOR_MALLOC在115ms内释放了839个对象/586800字节
01-05 22:12:15.312:E/GF\U UI\U T6626:E:I/O错误/mnt/sdcard/321.rar没有这样的文件或目录

bt如果sd卡只存在上传文件,你为什么不先检查一下sd卡呢!!!谢谢