Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓和发送“ü&引用;到服务器_Android_Json - Fatal编程技术网

Android 安卓和发送“ü&引用;到服务器

Android 安卓和发送“ü&引用;到服务器,android,json,Android,Json,我在使用json发送到服务器时遇到问题。我认为我在服务器上实现了所有好的广告,但有一些错误。这是代码: public void sendMail(List<String> addresses, String title, String contents, boolean addVcard){ JSONObject args = new JSONObject(); HashMap<Long, ArrayList<Long>> pd

我在使用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")){
         }

所以我认为一切正常,但服务器返回错误。我没有访问服务器的权限。您对此有何看法?

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

contents = new String(contents.getBytes("ISO-8859-1"), "UTF-8");
注意:您需要对所有数据执行此操作


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

使用UTF-8编码在服务器上发送数据。
contents = new String(contents.getBytes("ISO-8859-1"), "UTF-8");