Android中字符串中的特殊字符都变成了

时间:2015-05-11 23:17:36

标签: java android string encoding

我正在尝试将对象中的String变量赋值给一个值,该值可能包含丹麦字母,包括æ,ø和å。我正试图将它传递给我已经运行的服务器,但似乎所有这些字符在我将它们分配给字符串时就变成了thus,因此“HælløWærld”变成了“H ll ” WRLD”。字符编码是否默认为无法在Android中处理这些字符的内容?

上传字符串的代码:

            url = new URL(restUrl);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            OutputStream outputStream = urlConnection.getOutputStream();

            // TODO might have to change this to other charset to preserve æ ø å
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String test = getQuery(postParams);
            Log.d("Parametere", test);
            writer.write(test);
            writer.flush();
            writer.close();
            outputStream.close();

            responseCode = urlConnection.getResponseCode();
            urlConnection.disconnect();

getQuery的位置是:

    private String getQuery(ArrayList<NameValuePair> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}

处理字符串的服务器代码:

    public function createBookingReservation(BookingReservation $bookingReservation) {
    $date =                     $bookingReservation->getDate();
    $occasion =                 $bookingReservation->getOccasion();
    $guestId =                  $bookingReservation->getGuestId();
    $welcomeDrink =             $bookingReservation->getWelcomeDrink();
    $starters =                 $bookingReservation->getStarters();
    $mainCourse =               $bookingReservation->getMainCourse();
    $dessert =                  $bookingReservation->getDessert();
    $accessories =              $bookingReservation->getAccessories();
    $midnightSnack =            $bookingReservation->getMidnightSnack();
    $wine =                     $bookingReservation->getWine();
    $dessertWine =              $bookingReservation->getDessertWine();
    $cognacAndLiqueur =         $bookingReservation->getCognacAndLiqueur();
    $bar =                      $bookingReservation->getBar();
    $beerAndWater =             $bookingReservation->getBeerAndWater();
    $coffee =                   $bookingReservation->getCoffee();
    $tableArrangements =        $bookingReservation->getTableArrangements();
    $tableclothsColor =         $bookingReservation->getTableclothsColor();
    $candleColor =              $bookingReservation->getCandleColor();
    $tablePlan =                $bookingReservation->getTablePlan();
    $napkinColor =              $bookingReservation->getNapkinColor();
    $flowers =                  $bookingReservation->getFlowers();
    $musicEntertainment =       $bookingReservation->getMusicEntertainment();
    $notes =                    $bookingReservation->getNotes();
    $priceWelcomeDrink =        $bookingReservation->getPriceWelcomeDrink();
    $priceStarters =            $bookingReservation->getPriceStarters();
    $priceMainCourse =          $bookingReservation->getPriceMainCourse();
    $priceDessert =             $bookingReservation->getPriceDessert();
    $priceAccessories =         $bookingReservation->getPriceAccessories();
    $priceMidnightSnack =       $bookingReservation->getPriceMidnightSnack();
    $priceWine =                $bookingReservation->getPriceWine();
    $priceDessertWine =         $bookingReservation->getPriceDessertWine();
    $priceCognacAndLiqueur =    $bookingReservation->getPriceCognacAndLiqueur();
    $priceBar =                 $bookingReservation->getPriceBar();
    $priceBeerAndWater =        $bookingReservation->getPriceBeerAndWater();
    $priceCoffee =              $bookingReservation->getPriceCoffee();
    $priceTableArrangements =   $bookingReservation->getPriceTableArrangements();
    $priceTableClothsColor =    $bookingReservation->getPriceTableclothsColor();
    $priceCandleColor =         $bookingReservation->getPriceCandleColor();
    $priceTablePlan =           $bookingReservation->getPriceTablePlan();
    $priceNapkinColor =         $bookingReservation->getPriceNapkinColor();
    $priceFlowers =             $bookingReservation->getPriceFlowers();
    $priceMusicEntertainment =  $bookingReservation->getPriceMusicEntertainment();

    $query = "INSERT INTO " . DatabaseNames::$TABLE_BOOKING_RESERVATION;
    $query .= " VALUES( 'null',
                        '$date',
                        '$occasion',
                        '$guestId',
                        '$welcomeDrink',
                        '$starters',
                        '$mainCourse',
                        '$dessert',
                        '$accessories',
                        '$midnightSnack',
                        '$wine',
                        '$dessertWine',
                        '$cognacAndLiqueur',
                        '$bar',
                        '$beerAndWater',
                        '$coffee',
                        '$tableArrangements',
                        '$tableclothsColor',
                        '$candleColor',
                        '$tablePlan',
                        '$napkinColor',
                        '$flowers',
                        '$musicEntertainment',
                        '$notes',
                        '$priceWelcomeDrink',
                        '$priceStarters',
                        '$priceMainCourse',
                        '$priceDessert',
                        '$priceAccessories',
                        '$priceMidnightSnack',
                        '$priceWine',
                        '$priceDessertWine',
                        '$priceCognacAndLiqueur',
                        '$priceBar',
                        '$priceBeerAndWater',
                        '$priceCoffee',
                        '$priceTableArrangements',
                        '$priceTableClothsColor',
                        '$priceCandleColor',
                        '$priceTablePlan',
                        '$priceNapkinColor',
                        '$priceFlowers',
                        '$priceMusicEntertainment')";
    mysqli_query($this->con, $query);

调用
    private function handleBaseBookingReservation($requestMethod) {
    switch ($requestMethod) {
        case 'POST':
            $date =                     $_POST['date'];
            $occasion =                 $_POST['occasion'];
            $guestId =                  $_POST['guestId'];
            $welcomeDrink =             $_POST['welcomeDrink'];
            $starters =                 $_POST['starters'];
            $mainCourse =               $_POST['mainCourse'];
            $dessert =                  $_POST['dessert'];
            $accessories =              $_POST['accessories'];
            $midnightSnack =            $_POST['midnightSnack'];
            $wine =                     $_POST['wine'];
            $dessertWine =              $_POST['dessertWine'];
            $cognacAndLiqueur =         $_POST['cognacAndLiqueur'];
            $bar =                      $_POST['bar'];
            $beerAndWater =             $_POST['beerAndWater'];
            $coffee =                   $_POST['coffee'];
            $tableArrangements =        $_POST['tableArrangements'];
            $tableclothsColor =         $_POST['tableclothsColor'];
            $candleColor =              $_POST['candleColor'];
            $tablePlan =                $_POST['tablePlan'];
            $napkinColor =              $_POST['napkinColor'];
            $flowers =                  $_POST['flowers'];
            $musicEntertainment =       $_POST['musicEntertainment'];
            $notes =                    $_POST['notes'];
            $priceWelcomeDrink =        $_POST['priceWelcomeDrink'];
            $priceStarters =            $_POST['priceStarters'];
            $priceMainCourse =          $_POST['priceMainCourse'];
            $priceDessert =             $_POST['priceDessert'];
            $priceAccessories =         $_POST['priceAccessories'];
            $priceMidnightSnack =       $_POST['priceMidnightSnack'];
            $priceWine =                $_POST['priceWine'];
            $priceDessertWine =         $_POST['priceDessertWine'];
            $priceCognacAndLiqueur =    $_POST['priceCognacAndLiqueur'];
            $priceBar =                 $_POST['priceBar'];
            $priceBeerAndWater =        $_POST['priceBeerAndWater'];
            $priceCoffee =              $_POST['priceCoffee'];
            $priceTableArrangements =   $_POST['priceTableArrangements'];
            $priceTableclothsColor =    $_POST['priceTableclothsColor'];
            $priceCandleColor =         $_POST['priceCandleColor'];
            $priceTablePlan =           $_POST['priceTablePlan'];
            $priceNapkinColor =         $_POST['priceNapkinColor'];
            $priceFlowers =             $_POST['priceFlowers'];
            $priceMusicEntertainment =  $_POST['priceMusicEntertainment'];

            // Guest ID, date and dateTime required
            if($guestId == null || $date == null) {
                header('HTTP/1.1 400 Bad Request: Guest ID and date required');
            }

            $bookingReservation = new BookingReservation($date, $occasion, $guestId, $welcomeDrink, $starters, $mainCourse, $dessert,
                            $accessories, $midnightSnack, $wine, $dessertWine, $cognacAndLiqueur, $bar, $beerAndWater,
                            $coffee, $tableArrangements, $tableclothsColor, $candleColor, $tablePlan, $napkinColor,
                            $flowers, $musicEntertainment, $notes, $priceWelcomeDrink, $priceStarters, $priceMainCourse, $priceDessert,
                            $priceAccessories, $priceMidnightSnack, $priceWine, $priceDessertWine, $priceCognacAndLiqueur,
                            $priceBar, $priceBeerAndWater, $priceCoffee, $priceTableArrangements, $priceTableclothsColor,
                            $priceCandleColor, $priceTablePlan, $priceNapkinColor, $priceNapkinColor, $priceFlowers, $priceMusicEntertainment);

           $this->HCIModel->createBookingReservation($bookingReservation);
            break;

我也知道这很容易被SQL注入和所有好东西,但这是一个大学项目,所以我真的不太关心。

0 个答案:

没有答案