0

我在使用 json 向服务器发送 ü 时遇到问题。我认为我在服务器上实现了所有好的广告错误。这是代码:

public void sendMail(List<String> addresses, String title, String contents, boolean addVcard){
        JSONObject args = new JSONObject();
        HashMap<Long, ArrayList<Long>> pdfs = new HashMap<Long, ArrayList<Long>>();
        if(list == null)
            list = (EmailListFragment)getSupportFragmentManager().findFragmentById(R.id.list_fragment);
        if(list == null)
            list = (EmailListFragment)getSupportFragmentManager().findFragmentByTag(LIST_FRAGMENT);
        if(list != null){
            for(TreeFile pdf: list.getPDFs()){

                if(pdf.type == FilesLoader.PAGE){
                    ArrayList<Long> pages = pdfs.get(pdf.parent);
                    if(pages == null){
                        pages = new ArrayList<Long>();
                        pdfs.put(pdf.parent, pages);
                    }
                    pages.add(pdf.id);
                }else if(pdf.type == FilesLoader.PDF){
                    if(pdfs.get(pdf.id) == null)
                        pdfs.put(pdf.id, new ArrayList<Long>());
                }
            }
        }
        try {
            args.put("emails", new JSONArray(addresses));
            Iterator<Entry<Long, ArrayList<Long>>> it = pdfs.entrySet().iterator();
            JSONObject pdfsArgs = new JSONObject();
                // attachment added by user

            while (it.hasNext()) {
                Entry<Long, ArrayList<Long>> pairs = it.next();
                JSONArray pages = new JSONArray();

                // sorting pages by id
                ArrayList<Long> pageList = (ArrayList<Long>)pairs.getValue();
                Collections.sort(pageList);

                for(Long id: pageList){
                    pages.put(id+"");
                }
                if(pages.length() > 0)
                    pdfsArgs.put(pairs.getKey() + "", pages);
                it.remove(); // avoids a ConcurrentModificationException
            }
            args.put("magazines", pdfsArgs);
//          String mes = null;
//          try {
//              mes = URLEncoder.encode(contents, "CP1252");
//          } catch (UnsupportedEncodingException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
            args.put("message", contents);
            args.put("subject", title);
            if(getvca.emailPref != null && getvca.emailPref.length() > 0
                    && getvca.emailPref.matches("[a-zA-Z0-9._@+,; \\-]+"))
                args.put("sender", getvca.emailPref);
            else
                args.put("sender", GlobalConfig.getUser());

            if(addVcard){
                args.put("vcard", "1");
                getvca.addTo(args);
            }else{
                args.put("vcard", "0");
            }



        } catch (JSONException e) {
            MY_DEBUG.print(e);
        }

        postData = new SendPostData();
        postData.execute(args);
    }



    private class SendPostData extends AsyncTask<JSONObject, String, Long> {
        ByteArrayOutputStream out;
         @Override
        protected Long doInBackground(JSONObject... objects) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(GlobalConfig.getSendEmail());

            httppost.addHeader("Authorization", "Basic " + Base64.encodeToString(
                    (GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP));
            httppost.addHeader("Accept-Encoding", "CP1252");
            httppost.addHeader("Accept-Encoding", "UTF-8");
            try {
                MY_DEBUG.print(objects[0].toString(4));
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("data", objects[0].toString()));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();

                publishProgress(out.toString());
                setOut(out);
                MY_DEBUG.LOG(getClass(), "Email server response: " + out.toString());
                if(out != null && out.toString().contains("0"))
                    return (long) 0;
                else
                    return (long) 1;
            } catch (ClientProtocolException e) {
                MY_DEBUG.print(e);
            } catch (IOException e) {
                MY_DEBUG.print(e);
            } catch (JSONException e) {
                MY_DEBUG.print(e);
            }

            Long startPosition = (long) 1;
            return startPosition;
         }
        public void setOut(ByteArrayOutputStream out) {
            this.out = out;
        }

        @Override
        protected void onProgressUpdate(String... progress) {
//             if(progress[0].contains("0")){
         }

在日志中我看到:

01-16 13:12:42.167: E/ASDASD(27672): {
01-16 13:12:42.167: E/ASDASD(27672):     "position": "",
01-16 13:12:42.167: E/ASDASD(27672):     "phone": "",
01-16 13:12:42.167: E/ASDASD(27672):     "fax": "",
01-16 13:12:42.167: E/ASDASD(27672):     "subject": "",
01-16 13:12:42.167: E/ASDASD(27672):     "surname": "",
01-16 13:12:42.167: E/ASDASD(27672):     "firstname": "",
01-16 13:12:42.167: E/ASDASD(27672):     "emails": [
01-16 13:12:42.167: E/ASDASD(27672):         "test@test.pl"
01-16 13:12:42.167: E/ASDASD(27672):     ],
01-16 13:12:42.167: E/ASDASD(27672):     "vcard": "1",
01-16 13:12:42.167: E/ASDASD(27672):     "sender": "ediusz@gmail.com",
01-16 13:12:42.167: E/ASDASD(27672):     "message": "ü",
01-16 13:12:42.167: E/ASDASD(27672):     "email": "ediusz@gmail.com",
01-16 13:12:42.167: E/ASDASD(27672):     "company": "",
01-16 13:12:42.167: E/ASDASD(27672):     "magazines": {
01-16 13:12:42.167: E/ASDASD(27672):         "90": [
01-16 13:12:42.167: E/ASDASD(27672):             "1",
01-16 13:12:42.167: E/ASDASD(27672):             "2",
01-16 13:12:42.167: E/ASDASD(27672):             "3"
01-16 13:12:42.167: E/ASDASD(27672):         ]
01-16 13:12:42.167: E/ASDASD(27672):     },
01-16 13:12:42.167: E/ASDASD(27672):     "internet": "",
01-16 13:12:42.167: E/ASDASD(27672):     "mobile": ""
01-16 13:12:42.167: E/ASDASD(27672): }

所以我认为一切正常,但服务器返回错误。我无权访问服务器。你怎么看?

4

1 回答 1

1

您需要找出服务器使用的编码(可能/希望是UTF-8)并将您的内容从您使用的编码转换为正确的(服务器)编码:

contents = new String(contents.getBytes("ISO-8859-1"), "UTF-8");

注意:您需要对所有数据执行此操作。

如果您使用错误的编码,它可能(确实)导致服务器出错。

于 2013-01-16T12:19:50.217 回答