如何在android中使用httppost将Doc,Pdf,txt文件上传到服务器

时间:2016-11-24 10:45:58

标签: android http-post

我需要知道如何通过android将doc,txt,pdf文件上传到mysql服务器的完整源代码。下面的代码是将字符串上传到服务器,我需要知道我可以用这些nameValuePairs上传文件的方式。

 class PostJobAsync extends AsyncTask<String, String, String> {
    //String result;
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(getActivity(), "",
                "Submitting. Please wait...", true);
        dialog.show();
    }

    @Override


    protected String doInBackground(String... params) {

        InputStream is = null;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("postifontitle", positiontitle));
        nameValuePairs.add(new BasicNameValuePair("destcription", description));

        nameValuePairs.add(new BasicNameValuePair("technwical_skills", techskillsval));
        nameValuePairs.add(new BasicNameValuePair("sofrt_skills", softskillsval));
        nameValuePairs.add(new BasicNameValuePair("raate", rate));
        nameValuePairs.add(new BasicNameValuePair("posteedby", getUser_id));
        nameValuePairs.add(new BasicNameValuePair("hotjtob", hotjobornot));
        nameValuePairs.add(new BasicNameValuePair("starttdate", startdate));
        nameValuePairs.add(new BasicNameValuePair("dommain", domain));
        nameValuePairs.add(new BasicNameValuePair("rellocation", willingtorelocateval));
        //  if (selectedFilePath != null)
        // {
        //    nameValuePairs.add(new BasicNameValuePair("attachment", selectedFilePath));
        //   }

        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.3.2/Jummusapi/post-job");
            // HttpPost httpPost = new HttpPost("http://www.nthmile.com/xpressjobs/rest/postjob");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            int code = response.getStatusLine().getStatusCode();
            String rescode = String.valueOf(code);
            //result=is.toString();
            return rescode;
        } catch (MalformedURLException e) {
            return e.toString();
        } catch (IOException e) {
            return e.toString();
        }
    }

0 个答案:

没有答案