|
|
|
Android - ค้นหาภาษาไทยไม่ได้ ฐานข้อมูล mysql โดยใช้ Json ดึงข้อมูล |
|
|
|
|
|
|
|
ช่วยแสดง โค๊ด ที่ใช้ค้นหา ภาษาไทย ด้วยครับ
|
|
|
|
|
Date :
2014-06-16 06:55:45 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดูพวก Encoding เป็นแบบ UTF-8 ก็ได้แล้วครับ แต่จะต้องทำบน Android และ PHP/MySQL ด้วยครับ
Android MySQL ภาษาไทย มีปัญหากับการส่งค่าแบบ HttpPost
|
|
|
|
|
Date :
2014-06-16 11:12:10 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code ส่วนที่เป็นหน้าค้นหาครับ
Search_th.java
Code (Android-Java)
package com.example.last;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
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.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Search_th extends Activity {
ArrayList<HashMap<String, String>> MyArrList;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_th);
setTitle("Medicinal plants and herbs");
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ShowData();
// btnSearch
final Button btnSearch = (Button) findViewById(R.id.btnSearch);
//btnSearch.setBackgroundColor(Color.TRANSPARENT);
// Perform action on click
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//btnSearch.setText(strKeySearch.getText());
ShowData();
}
});
}
public void ShowData()
{
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// keySearch
EditText strKeySearch = (EditText)findViewById(R.id.txtKeySearch);
// Disbled Keyboard auto focus
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(strKeySearch.getWindowToken(), 0);
String ip = "nap21.fulba.com";
String url = "http://"+ip+"/json/showAllDatapy.php";
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
String txtKeyword = strKeySearch.getText().toString();
params.add(new BasicNameValuePair("txtKeyword", txtKeyword));
JSONArray data = new JSONArray(getJSONUrl(url, params));
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("herb_id", c.getString("herb_id"));
map.put("herb_name", c.getString("herb_name"));
map.put("name_sci", c.getString("name_sci"));
map.put("herb_gene", c.getString("herb_gene"));
map.put("herb_fix", c.getString("herb_fix"));
map.put("herb_pic", c.getString("herb_pic"));
MyArrList.add(map);
}
lisView1.setAdapter(new ImageAdapter(this));
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int position, long mylng) {
String herb_id = MyArrList.get(position).get("herb_id")
.toString();
Intent newActivity = new Intent(Search_th.this,DetailActivity_th.class);
newActivity.putExtra("herb_id", herb_id);
startActivity(newActivity);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Json", e.toString());
//e.printStackTrace();
}
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
// TODO Auto-generated method stub
context = c;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column_th, null);
}
// ColMemberID
TextView txtMemberID = (TextView) convertView.findViewById(R.id.ColMemberID);
txtMemberID.setPadding(10, 0, 0, 0);
txtMemberID.setText(MyArrList.get(position).get("herb_id") +".");
// R.id.ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText(MyArrList.get(position).get("herb_name"));
// R.id.ColTel
return convertView;
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
อันนี้ Code PHP ที่เป็น Json ครับ
Code (PHP)
<?php
$objConnect=mysql_connect("localhost","root", "abc1234");
$objDB = mysql_select_db("test");
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
// $_POST["txtKeyword"] = "a"; // for Sample
$strKeyword = $_POST["txtKeyword"];
$strSQL = "SELECT * FROM herb_data_th WHERE herb_name LIKE '%".$strKeyword."%' ";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
//echo $strKeyword."";
echo json_encode($resultArray);
?>
|
|
|
|
|
Date :
2014-06-18 04:28:34 |
By :
madrid_utd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองแล้วครับยังไม่ได้ แสดงผลภาษาไทยปกติครับ ค้นหาภาษาอังกฤษได้ " ค้นหาภาษาไทยไม่ได้ครับ "
|
|
|
|
|
Date :
2014-06-21 01:25:55 |
By :
madrid_utd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับ ขอบคุณมาก
ลืมใส่
Code (Android-Java)
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
|
|
|
|
|
Date :
2014-06-21 01:40:37 |
By :
madrid_utd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2014-06-21 08:52:11 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|