2013-01-16 52 views
0

我在使用json發送服務器時遇到問題。我認爲我在服務器上實現了一些很好的錯誤廣告。這是代碼:Android並向服務器發送「ü」

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("[[email protected]+,; \\-]+")) 
       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):   "[email protected]" 
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": "[email protected]", 
01-16 13:12:42.167: E/ASDASD(27672):  "message": "ü", 
01-16 13:12:42.167: E/ASDASD(27672):  "email": "[email protected]", 
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): } 

所以我認爲一切都很好,但服務器返回的錯誤我。我無法訪問服務器。你對此有何看法?

+0

使用UTF-8編碼在服務器上發送數據。 – Kamal

回答

1

你需要找出編碼服務器使用(可能是/希望UTF-8),並從您使用的編碼轉換您的內容以正確的(服務器)編碼:

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

注意:您你需要爲你的所有數據這樣做。

如果使用錯誤的編碼,它可能(確實)會導致服務器發生錯誤。