0

I try send data to server but when I send Persian words it sends question marks, for example if I send "سلام" it sends "????"

How can I fix this ?

This is my FragmentForm.class :

package com.skyline.jimmy;

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 java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import com.skyline.jimmy.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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));
                //URL url = new URL(LinkManager.getFormAPI(deviceId, name, rate, comment));
                String url1 = LinkManager.getFormAPI(deviceId, name, rate, comment) ;
                //String stUrl = URLEncoder.encode(url1, "UTF-8");
                URL url = new URL(url1);

                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();
        }
    }
}

and my LinkManager.class :

package com.skyline.jimmy;

public class LinkManager {

    private final static String API_FORM = "http://jimmy.ir/jimmy/sendjoke.php?p1=#@#&p2=#@@#&p3=#@@@#&p4=#@@@@#";
    private final static String API_Comment = "http://jimmy.ir/jimmy/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);


        return url;
    }

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

        return url;
    }
}
Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
Aryan Bm
  • 21
  • 5
  • You should change the encoding that the server accepts and the encoding that your text has in your request – Pavlos Sep 27 '14 at 14:34
  • @Pavlos tnx bro , but how and where in my code :D – Aryan Bm Sep 27 '14 at 14:36
  • http://stackoverflow.com/questions/6227747/android-default-charset-when-sending-http-post-put-problems-with-special-chara Check this link! – Pavlos Sep 27 '14 at 14:38
  • 1
    `I try send data to server``. Please the next time tell right away that you try to put persian characters in the parameters of an url. That saves time of those who want to help and had to dig throug a lot of code now. – greenapps Sep 27 '14 at 17:27

2 Answers2

0

See this link for accepted characters in URL. Persian characters are not supported as get parameters in a url. You can use http post for sending the data to the server.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0

first make sure that you are sending right word (persian ) from android side , maybe your java code not sending text in UTF-8 , then make sure that your server, database , and table collection are utf8_persian_ci

and change this

BufferedReader in = new BufferedReader(
                        new InputStreamReader(urlConnection.getInputStream()));

to this:

BufferedReader in = new BufferedReader(
             new InputStreamReader(urlConnection.getInputStream(), "utf-8"),8);
Samad
  • 1,776
  • 2
  • 20
  • 35