如何使用asynctask和字符串发送文件?

时间:2018-07-24 13:55:08

标签: android file-upload android-asynctask

初学者在这里。

我正在尝试使用asyncTask将数据从android发送到PHP页面。只能处理字符串是可以的,但是我不知道如何使用AsyncTask发送带有字符串的文件。

这是我的AsyncTask类

public class BackGroundWorker extends AsyncTask<String ,File,String >{

private AlertDialog.Builder alertDialog;

private Context context;

public BackGroundWorker(Context context) {
    this.context = context;
}

@Override
protected String doInBackground(String ... params) {
    String type = params[0];
    if (type.equals(Utils.TYPE_SIGN_UP)){
        try {
            URL url = new URL(Utils.URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);

            OutputStream outputStream = connection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

            String post_data = URLEncoder.encode(Utils.KEY_NAME, "UTF-8")+"="+URLEncoder.encode(params[1], "UTF-8")+"&"+
                    URLEncoder.encode(Utils.KEY_EMAIL, "UTF-8")+"="+URLEncoder.encode(params[2], "UTF-8")+"&"+
                    URLEncoder.encode(Utils.KEY_PHONE, "UTF-8")+"="+URLEncoder.encode(params[3], "UTF-8")+"&"+
                    URLEncoder.encode(Utils.KEY_PASSWORD, "UTF-8")+"="+URLEncoder.encode(params[4], "UTF-8");

            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

            String result = "";
            String line = "";

            while ((line = bufferedReader.readLine()) != null){
                result += line;
            }

            bufferedReader.close();
            inputStream.close();
            connection.disconnect();

            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context);
    alertDialog.create();
    alertDialog.setTitle("Registration Status");
}

@Override
protected void onPostExecute(String  aVoid) {
    alertDialog.setMessage(aVoid);
    alertDialog.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    alertDialog.show();
}

@Override
protected void onProgressUpdate(File ... values) {
    super.onProgressUpdate(values);
}

我从MainActivity.java调用它

worker = new BackGroundWorker(context);
                    worker.execute(Utils.TYPE_SIGN_UP, name, email, phone, password);

如何以同时处理字符串和文件的方式实现它?因为当我尝试在此处发送文件时:

worker.execute(Utils.TYPE_SIGN_UP, name, email, phone, password, image_file);

我得到一个错误。

1 个答案:

答案 0 :(得分:0)

我认为使用第三方进行联网可以使您的生活更轻松。

Retrofit将使您在这种情况下更容易维护。