Answer the question
In order to leave comments, you need to log in
How to solve the problem with the encoding when sending an email?
There is an application, in it a contact form, when receiving a letter to the post office, the Russian language is in the form of hieroglyphs.
How to set encoding?
public class ContactActivity extends AppCompatActivity {
private static final String TAG = ContactActivity.class.getSimpleName();
private ProgressDialog pDialog;
private Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
activity = this;
pDialog = new ProgressDialog(activity);
final EditText your_name = (EditText) findViewById(R.id.your_name);
final EditText your_email = (EditText) findViewById(R.id.your_email);
final EditText your_subject = (EditText) findViewById(R.id.your_subject);
final EditText your_message = (EditText) findViewById(R.id.your_message);
//Forcing RTL Layout, If Supports and Enabled from Const file
Utils.forceRTLIfSupported(this);
//Setting Up Support Toolbar
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(getString(R.string.contact_form));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
Button post_message = (Button) findViewById(R.id.post_message);
post_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean onError = false;
final String email = your_email.getText().toString();
if (!isValidEmail(email)) {
onError = true;
your_email.setError(getString(R.string.error_invalid_email));
}
final String name = your_name.getText().toString();
if (!isValidString(name, Const.ContactNameValidationLimit)) {
onError = true;
your_name.setError(String.format(getString(R.string.error_length), Integer.toString(Const.ContactNameValidationLimit)));
}
final String subject = your_subject.getText().toString();
if (!isValidString(subject, Const.ContactSubjectValidationLimit)) {
onError = true;
your_subject.setError(String.format(getString(R.string.error_length), Integer.toString(Const.ContactSubjectValidationLimit)));
}
final String message = your_message.getText().toString();
if (!isValidString(message, Const.ContactMessageValidationLimit)) {
onError = true;
your_message.setError(String.format(getString(R.string.error_length), Integer.toString(Const.ContactMessageValidationLimit)));
}
if (!onError) {
pDialog.setTitle(getString(R.string.contact_form));
pDialog.setMessage(getString(R.string.contact_form_post_message));
pDialog.show();
//Requesting The Story
String url = Const.URL_BLOG + Const.URL_BLOG_API + Const.URL_SEND_MESSAGE + "?t=" + System.currentTimeMillis();
// making fresh volley request and getting json
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pDialog.hide();
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject) jsonParser.parse(response);
try {
JSONObject obj = new JSONObject(response);
if (obj.has("error")) {
String error = obj.getString("error");
Toast.makeText(activity, error, Toast.LENGTH_LONG).show();
} else {
//parseJsonFeed(response);
your_message.setText("");
your_name.setText("");
your_email.setText("");
your_subject.setText("");
Utils.showAlertDialog(activity, getString(R.string.contact_form), getString(R.string.contact_posted));
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("contact_name", name);
params.put("contact_email", email);
params.put("contact_subject", subject);
params.put("contact_message", message);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("ApiKey", Const.AuthenticationKey);
return params;
}
};
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(sr);
}
}
});
}
@Override
public void onResume() {
super.onResume();
//Get a Tracker (should auto-report)
if (Const.Analytics_ACTIVE) {
AnalyticsUtil.sendScreenName(this, TAG);
}
}
@Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(ContactActivity.this).reportActivityStart(this);
}
@Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(ContactActivity.this).reportActivityStop(this);
}
// validating email id
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
// validating information
private boolean isValidString(String pass, int length) {
if (pass != null && pass.length() > length) {
return true;
}
return false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question