How to send Hindi text to php server using HttpUrlConnection in Android

I am developing an Android application in which a user can register by choosing Hindi and English. It works great when sending to English, but when sending Hindi text, I get text in t format %E0%A4%A6%E0%A4%95%E0%A4%97in the table. I tried to put the encoding for the header, but it does not work.

The code I use

URL url = new URL(Config.ORGANISATION_DETAILS_URL);

// Open a HTTP  connection to  the URL
conn = (HttpURLConnection) url.openConnection(); 
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;charset=utf-8;boundary=" + boundary);

// conn.setRequestProperty("Expect", "100-continue");
    conn.setRequestProperty("uploaded_file", fileName); 
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);

//Adding Parameter name of organisation
String str=nameoforget.getText().toString();
String nameoforg=URLEncoder.encode(str, "UTF-8");
//String nameoforg=str;
dos.writeBytes("Content-Disposition: form-data; name=\"nameoforg\"" + lineEnd); 
//dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
//dos.writeBytes("Content-Length: " + nameoforg.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(nameoforg); // mobile_no is String variable
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);

String noofstudents=""+noofstdset.getText().toString();
dos.writeBytes("Content-Disposition: form-data; name=\"noofstudents\"" + lineEnd); 
//dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
//dos.writeBytes("Content-Length: " + noofstudents.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(noofstudents); // mobile_no is String variable
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);

/*To upload file*/ 
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);

dos.writeBytes(lineEnd);
if(sourceFileUri!=null)
{
    // create a buffer of  maximum size
    bytesAvailable = fileInputStream.available(); 

    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // read file and write it into form...
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

    while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);   
    }
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

Thanks at Advance ..

+4
source share
2 answers

How to send Hindi text or other encoded text to a Php server using HttpUrlConnection in Android

    After searching in google i got the solution for my question. Before sending the data I Encoded the String with URLEncoder.encode() in java class. The following code is in java

In Java:

     String nameoforg=URLEncoder.encode("हिन्दी", "UTF-8");
     dos.writeBytes("Content-Disposition: form-data; name=\"nameoforg\"" +  lineEnd); 
     dos.writeBytes(nameoforg); 
     dos.writeBytes(lineEnd);
     dos.writeBytes(twoHyphens + boundary + lineEnd);

And now in PHP, when we get the Post data from Android, we need to decode the post.For data For example

In php:

    $nameoforg=$_POST['nameoforg'];
    $nameoforg=urldecode($nameoforg);
    echo $nameoforg;

... php.

0

,

encodeString(your hindi or english string)

public static String encodeString(String str) throws UnsupportedEncodingException {
  return URLEncoder.encode(str, "UTF-16");
}

sever

decodeString(your server string)

public static String decodeString(String str) throws UnsupportedEncodingException {
    return URLDecoder.decode(str, "UTF-16");
}
0

All Articles