发送到Web的字符串不支持utf 8

时间:2014-09-07 23:13:26

标签: java android utf-8

我尝试向网络发送一些文字,这是我的代码,但它不支持utf8 例如آرین Show ???? 我怎么能删除这个问题?! 这支持英语...

FragmentForm 



package com.kamalan.phpandroidapi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentForm extends Fragment {

    // An interface to display or dismiss of ProgressBar 
    public interface OnSendingRequestToServer {
        public void DisplayLoding(boolean setVisibility);
    }

    private final String TAG = "FragmentForm";

    private OnSendingRequestToServer onRequestToServer;
    private Context context;
    private EditText etName;
    private EditText etComment;
    private RatingBar ratingBar;
    private ImageButton ibSubmit;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            onRequestToServer = (OnSendingRequestToServer) activity;
        } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() + " must implement OnConnectingToServer interface.");
        }

        context = activity.getApplicationContext();

        Log.d(TAG, "Fragment attached to activity.");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_form, container, false);     
        etName    = (EditText) view.findViewById(R.id.etName);
        etComment = (EditText) view.findViewById(R.id.etComment);
        ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
        ibSubmit  = (ImageButton) view.findViewById(R.id.ibSubmit);
        //TextView tvcm  = (TextView) view.findViewById(R.id.tvComment);

        Log.d(TAG, "Fragment created.");
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ibSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                String strName = etName.getText().toString().trim();

                if(strName.length() <= 0) {
                    Toast.makeText(context, "نام خود را وارد کنید", Toast.LENGTH_LONG).show();
                    return;
                }

                String strComment = etComment.getText().toString().trim();
                if(strComment.length() <= 0) {
                    Toast.makeText(context, "متن جک را وارد کنید", Toast.LENGTH_LONG).show();
                    return;
                }

                int rate = (int) ratingBar.getRating();
                if(rate <= 0) {
                    Toast.makeText(context, "امتیاز جکتان را وارد کنید", Toast.LENGTH_LONG).show();
                    return;
                }

                String deviceId = getDeviceId();

                new SendFormTask(deviceId, strName, rate, strComment).execute();





            }

            private TextView findViewById(int tvcomment) {
                // TODO Auto-generated method stub
                return null;
            }
        });
    }

    private String getDeviceId() {
        return Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    }

    /*----------------------------------------------------------------------------
     * This method is responsible for creating another thread in parallel with
     * main UI thread in order to send a request to server and get data (if any).
     * ---------------------------------------------------------------------------*/
    public class SendFormTask extends AsyncTask<Void, Void, Boolean> {      
        String deviceId, name, comment;
        int rate;

        SendFormTask(String deviceId, String strName, int rate, String strComment) {
            this.deviceId = deviceId;
            this.name = strName;
            this.rate = rate;
            this.comment = strComment;
        }

        @Override
        protected void onPreExecute() {
            Log.d(TAG, "SendFormTask is about to start...");

            onRequestToServer.DisplayLoding(true);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean status = false; 
            HttpURLConnection urlConnection = null;

            try {
                URL url = new URL(LinkManager.getFormAPI(deviceId, name, rate, comment));
                Log.d(TAG, "Try to open: " + url.toString());
                urlConnection = (HttpURLConnection) url.openConnection();
                int responseCode = urlConnection.getResponseCode();
                Log.d(TAG, "Response code is: " + responseCode);

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    if (in != null) {
                        StringBuilder strBuilder = new StringBuilder();
                        // Read character by character              
                        int ch = 0;
                        while ((ch = in.read()) != -1)
                            strBuilder.append((char) ch);

                        // get returned message and show it
                        String response = strBuilder.toString();
                        Log.d("Server response:", response);

                        if(response.equalsIgnoreCase("1"))
                            status = true;
                    }

                    in.close();
                }
            }
            catch(MalformedURLException e){
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                urlConnection.disconnect();
            }

            return status;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            Log.d(TAG, "SendFormTask finished its task.");

            onRequestToServer.DisplayLoding(false);

            if(result)
                Toast.makeText(context, "جک شما ارسال شد, منتظر تایید آن باشید", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(context, "جک شما ارسال شد , منتظر تایید آن باشید", Toast.LENGTH_LONG).show();
        }
    }
}

LinkManager.java用于链接到网络

public class LinkManager {

    private final static String API_FORM = "http://example.com/sendjoke.php?p1=#@#&p2=#@@#&p3=#@@@#&p4=#@@@@#";
    private final static String API_Comment = "http://example.com/index.php?p1=#@#";

    public static String getFormAPI(String deviceId, String name, int rate, String comment) {
        String url = API_FORM;
        url = url.replaceAll("#@#", deviceId);
        url = url.replaceAll("#@@#", name);
        url = url.replaceAll("#@@@#", Integer.toString(rate));
        url = url.replaceAll("#@@@@#", comment);
        url = url.replaceAll(" ", "%20");

        return url;
    }

    public static String getCommentAPI(String deviceId) {
        String url = API_Comment;
        url = url.replaceAll("#@#", deviceId);

        return url;
    }
}

请帮助我。

Sry我的英语不好。

1 个答案:

答案 0 :(得分:0)

尝试使用urlencoding。喜欢:HttpGet get = new HttpGet(URLEncoder.encode(url,“UTF-8”)); url是特定的urllink。如果你想使用httppost,请在setEntity之前对实体进行编码