|
|
|
Android สอบถามเรื่องการส่งข้อมูลเข้าฐานข้อมูล MySQL ว่าทำไมพิพพ์ภาษาไทยแล้วมันไม่เป็นภาษาไทย |
|
|
|
|
|
|
|
สวัสดีคับ คือ อยากสอบถามหน่อยคับว่าผมเขียนแอพ ที่ส่งข้อมูลจากตัวแอพเข้าฐานข้อลูม SQL Server แล้วเวลาพิมพ์ภาษาไทยเข้าไปมันไม่เป็นภาษาไทย
หน้า (MainActivity)
Code (Android-Java)
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainActivity6 extends Activity {
private Button home;
EditText ET_NAME,ET_TEL,ET_SUBJECT,ET_MESSAGE;
String name,tel,subject,message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
home = (Button)findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
ET_NAME = (EditText)findViewById(R.id.name);
ET_TEL = (EditText)findViewById(R.id.tel);
ET_SUBJECT = (EditText)findViewById(R.id.subject);
ET_MESSAGE = (EditText)findViewById(R.id.message);
}
public void userReg(View view)
{
name = ET_NAME.getText().toString();
tel = ET_TEL.getText().toString();
subject = ET_SUBJECT.getText().toString();
message = ET_MESSAGE.getText().toString();
String method = "register";
//BackgroundTask backgroundTask = new BackgroundTask(this);
//backgroundTask.execute(method,name,tel,subject,message);
BackgroundTask_2 backgroundTask_2 = new BackgroundTask_2(this,name,tel,subject,message);
backgroundTask_2.get_Data();
//int a = insertUser(name,tel,subject,message);
Toast.makeText(getApplicationContext(), name+"\n"+tel+"\n"+subject+"\n"+message+"\n", Toast.LENGTH_SHORT).show();
finish();
}
public static int insertUser(final String name, final String tel, final String subject, final String message){ //------------ Method to insert data ---------------//
final InputStream[] is = {};
String js_result = "";
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
// Define Data
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user",name));
nameValuePairs.add(new BasicNameValuePair("tel",tel));
nameValuePairs.add(new BasicNameValuePair("subject",subject));
nameValuePairs.add(new BasicNameValuePair("message",message));
// Connect Server
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.178.1/webapp/register2.php"); // https://10.0.2.2/
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is[0] = entity.getContent();
} catch (Exception e) {
Log.d("log_err", "Error in http connection " + e.toString());
}
}
});
thread.start();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is[0],"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is[0].close();
js_result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
return -1;
}
int id = -1;
try {
final JSONArray jArray = new JSONArray(js_result);
for (int i = 0; i < jArray.length(); i++) {
String[] comment = new String[3];
JSONObject jo = jArray.getJSONObject(i);
id = Integer.parseInt((jo.get("id").toString()));
//tv_res.append(hn+","+name+","+age+","+date_serv+"\n");
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
return -1;
}
return id;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity6, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Tag : Java, MySQL, Android, JAVA
|
|
|
|
|
|
Date :
2016-02-03 01:05:27 |
By :
nutkonubon |
View :
1297 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(หน้า MainActivity)
Code (XML)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.asus.cis_project.MainActivity6"
android:background="@drawable/logo">
<TextView android:text="ฝากคำถาม" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:textStyle="bold"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView196" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="ส่งข้อมูล"
android:onClick="userReg"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/message"
android:layout_alignEnd="@+id/message" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="กลับ"
android:id="@+id/home"
android:layout_alignParentBottom="true" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:hint="ชื่อ-นามสกุล"
android:id="@+id/name"
android:layout_below="@+id/textView196"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:hint="เบอร์โทร"
android:id="@+id/tel"
android:layout_below="@+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:hint="เรื่อง"
android:id="@+id/subject"
android:layout_below="@+id/tel"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="350dp"
android:layout_height="100dp"
android:hint="ข้อความ"
android:id="@+id/message"
android:gravity="top"
android:layout_below="@+id/subject"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
|
|
|
|
|
Date :
2016-02-03 01:05:54 |
By :
nutkonubon |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
หน้า (BackgroundTask)
Code (Android-Java)
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.Toast;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class BackgroundTask_2 extends AppCompatActivity {
Context context;
String name1, tel1, subject1, message1;
public BackgroundTask_2(Context context,String name, String tel, String subject, String message) {
this.context = context;
this.name1 = name;
this.tel1 = tel;
this.subject1 = subject;
this.message1 = message;
}
public void get_Data() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
OkHttpClient okHttpClient = new OkHttpClient();
String name = convertToUTF8(name1);
String tel = convertToUTF8(tel1);
String subject = convertToUTF8(subject1);
String message = convertToUTF8(message1);
Request.Builder builder = new Request.Builder();
Request request = builder.url("http://192.168.178.1/webapp/addDATA.php?user="
+name+"&tel="+tel+"&subject="+subject+"&message="+message).build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
return "Not Success - code : " + response.code();
}
} catch (IOException e) {
e.printStackTrace();
return "Error - " + e.getMessage();
}
}
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
// Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
}
}.execute();
}
public static String convertToUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
}
|
|
|
|
|
Date :
2016-02-03 01:06:16 |
By :
nutkonubon |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(ส่วน PHP)
Code (PHP)
<?php
header("content-type:text/javascript;charset=utf-8");
$con=mysql_connect('localhost','root','1234')or die(mysql_error());
mysql_select_db('webappdb')or die(mysql_error());
mysql_query("SET NAMES UTF8");
$name = $_GET['user'];
$tel = $_GET['tel'];
$subject = $_GET['subject'];
$message = $_GET['message'];
$sql = "INSERT INTO user (name, tel, subject, message) values('$name','$tel','$subject','$message')";
$res=mysql_query($sql);
$arr = array('id' => mysql_insert_id()."");
print('['.json_encode($arr).']');
mysql_close();
?>
|
|
|
|
|
Date :
2016-02-03 01:07:06 |
By :
nutkonubon |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จากที่ดูแล้วก็น่าจะครบแล้วน่ะครับเช่น
Code (Android-Java)
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
Code (PHP)
mysql_query("SET NAMES UTF8");
ตัว Table ใช้ Collation แบบไหนครับ
|
|
|
|
|
Date :
2016-02-03 09:41:54 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ตัวนี้คับ utf8_unicode_ci
|
|
|
|
|
Date :
2016-02-03 11:25:32 |
By :
nutkonubon |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูจากกระทู้นี้ครับ ทบทวนว่าอะไรที่มันหายไป
URL
|
|
|
|
|
Date :
2016-02-03 13:35:59 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|