从Android应用程序发送文本和pdf文件到服务器

时间:2017-03-27 12:43:36

标签: android pdf server

当用户点击按钮时,我尝试将一组编辑文本和PDF文件发送到服务器。我成功地单独发送了一组文本而没有pdf,然后我成功地发送了没有文本的pdf。我想在同一个网址上同时发送它们两个如何做到这一点          文字代码

      private class postData extends AsyncTask<String, Void, String> {

      // private final ProgressDialog dialog = ProgressDialog.show(getActivity(), "",
     //       "Saving data to server. Please wait...", true);
    EditText email = (EditText) myView.findViewById(R.id.email);
    EditText phone = (EditText) myView.findViewById(R.id.email);
    String eemail=email.getText().toString();
    String pphone=phone.getText().toString();
    EditText first  = (EditText) myView.findViewById(R.id.firstname);
    EditText second= (EditText) myView.findViewById(R.id.lastname);
    String firstname=first.getText().toString();
    String lastname=second.getText().toString();

    @Override
    protected String doInBackground(String... params) {
        // perform long running operation operation

        // SharedPreferences settings = context.getSharedPreferences(PREFS_FILE, 0);
        //String server = settings.getString("server", "");
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://phone.tmsline.com/api/request_job");
        String json = "";
        String responseStr="";
        String result="true";


        try {
            // Add your data

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("email", eemail));
            nameValuePairs.add(new BasicNameValuePair("phone", pphone));
            nameValuePairs.add(new BasicNameValuePair("cv","not now"));
            nameValuePairs.add(new BasicNameValuePair("firstname", firstname));
            nameValuePairs.add(new BasicNameValuePair("lastname", lastname));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            try {
                httpclient.execute(httppost);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }



        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("HTTP Failed", e.toString());
        }
        return result;
    }
    protected void onPostExecute(String responseStr) {
        super.onPostExecute(responseStr);
        Toast.makeText(getActivity(),responseStr,Toast.LENGTH_LONG).show();

        if(responseStr.equals("true")){
            // Update your Button here
          Toast.makeText(getActivity(),"done finally",Toast.LENGTH_LONG).show();
        }

    }
}

pdf代码

public  void filetest(){
    String url = "http://phone.tmsline.com/api/";
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
            "Sherouk adel.pdf");
    try {
        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(url);

        InputStreamEntity reqEntity = new InputStreamEntity(
                new FileInputStream(file), -1);
        reqEntity.setContentType("binary/octet-stream");
        reqEntity.setChunked(true); // Send in multiple parts if needed
        httppost.setEntity(reqEntity);
        //HttpResponse response = httpclient.execute(httppost);
        //Do something with response...

    } catch (Exception e) {
        // show error
    }

1 个答案:

答案 0 :(得分:0)

使用它可以更好地检查Async Task,而不是使用所有这些,您可以将EditText数据PDF一起发送,否则您可以先发送EditText数据,然后在postExecute方法中发送PDF。