在android

时间:2017-08-29 12:46:06

标签: android

在我的应用程序中,我想让用户以pdf / word格式上传他的简历,并且还想让用户上传他的照片。我可以浏览照片和简历,但无法使用其他参数

发送到单个请求的PHP服务器

获取照片的方法

public void getPhoto(){
        Intent photoIntent = new Intent(Intent.ACTION_PICK);
        photoIntent.setType("image/*");
        startActivityForResult(photoIntent, REQUEST_GALLERY);
    }

获取文档的方法

public void getDocument(){
        Intent docIntent = new Intent(Intent.ACTION_GET_CONTENT);
        docIntent.setType("application/msword,application/pdf");
        docIntent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(docIntent, REQUEST_DOCUMENT_CODE);
    }

关于活动结果代码

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK)
        {
            if(requestCode == IMG)
                imgUrI = data.getData();
            if(requestCode == CV)
                docURI = data.getData();
        }
        else {
            Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
        }
    }

我想用其他参数上传简历和照片,例如

protected String doInBackground(String... params) {
        try{
            String url="someurl.php";
            URL url = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data=
                      URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(params[0], "UTF-8") + "&"
                    + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(params[1], "UTF-8") + "&"
                    + URLEncoder.encode("gender", "UTF-8") + "=" + URLEncoder.encode(params[2], "UTF-8")+ "&"
                    + URLEncoder.encode("resume", "UTF-8") + "=" + URLEncoder.encode(params[3], "UTF-8")+ "&"
                    + URLEncoder.encode("photo", "UTF-8") + "=" + URLEncoder.encode(params[4], "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

            while ((string = bufferedReader.readLine()) != null) {
                stringBuilder.append(string+"\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
        }
        catch (Exception e){
            Toast.makeText(Register.this, "Error: "+e, Toast.LENGTH_SHORT).show();
        }
        return stringBuilder.toString();
    }

我的3和4参数应该是pdf / word或image,这是最简单的方法。

0 个答案:

没有答案