|
|
|
Android - ส่งข้อมูลจากโทรศัพท์มือถือ ไปยัง server เพื่อ insert ข้อมูลลง database ครับ ไม่ error แต่ insert ไม่ได้ครับ |
|
|
|
|
|
|
|
นี่ code ฝั่งclient android mobile ครับ
package th.ac.kmutnb.reachtotemple;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import android.graphics.Color;
public class Recommend extends Activity{
private static Button start_date;
private static Button end_date;
private EditText from;
private EditText content;
private final static int START_DATE = 999;
private final static int END_DATE = 998;
private static int start_year;
private static int start_month;
private static int start_day;
private static int end_year;
private static int end_month;
private static int end_day;
private static boolean start_flag = true;
private static boolean end_flag = true;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recommend);
start_date = (Button)findViewById(R.id.start_date);
end_date = (Button)findViewById(R.id.end_date);
from = (EditText)findViewById(R.id.rec_edit_from);
content = (EditText)findViewById(R.id.rec_content);
}
public void showDatePickerDialog_start(View v) {
DialogFragment newFragment = new DatePickerFragment(START_DATE);
newFragment.show(getFragmentManager(), "datePicker");
}
public void showDatePickerDialog_end(View v) {
DialogFragment newFragment = new DatePickerFragment(END_DATE);
newFragment.show(getFragmentManager(), "datePicker");
}
public void rec_ok(View v){
//check EditText FROM
boolean check_from_user = true;
String from_user = String.valueOf(from.getText());
from_user = from_user.trim();
for(int i = 0;i < from_user.length();i++){
char ch;
ch = from_user.charAt(i);
if(ch == '\'' || ch == '\"'){
Log.d("CHAR" , ""+ch);
check_from_user = false;
break;
}
}
//check EditText CONTENT
boolean check_content = true;
String content_user = String.valueOf(content.getText());
content_user = content_user.trim();
for(int i = 0;i < content_user.length();i++){
char ch;
ch = content_user.charAt(i);
if(ch == '\'' || ch == '\"'){
Log.d("CHAR" , ""+ch);
check_content = false;
break;
}
}
String str = "^_^";
if(start_flag && end_flag && check_from_user && !from_user.equals("") && check_content && !content_user.equals("")){
if(isConnectInternet()){
String Strstart_date = null;
String Strend_date = null;
if(start_year != 0){
Strstart_date = start_year + "-" + start_month + "-" + start_day;
Strend_date = end_year + "-" + end_month + "-" + end_day;
}
//Send to Server
HttpPostTask postTask = new HttpPostTask(mPostCallback_post);
HttpPostTask.Parameter param = new HttpPostTask.Parameter();
HashMap<String, String> postMap = new HashMap<String, String>();
param.post = postMap;
param.url = "http://" + "192.168.1.3" + "/activity/" + "postactivity.php";
//Send parameter to server
postMap.put("Strfrom", from_user);
postMap.put("Strcontent", content_user);
postMap.put("Strstart_date", Strstart_date);
postMap.put("Strend_date", Strend_date);
postTask.execute(param);
}else{
Toast.makeText(getApplicationContext(),"please connect internet", Toast.LENGTH_LONG).show();
}
}else if(!check_from_user){
str = "don't use (\" \') character in user name";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}else if(from_user.equals("")){
str = "please insert user name";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}else if(start_flag == false || end_flag == false){
str = "please choose final activity date";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}else if(content_user.equals("")){
str = "please insert content";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}else if(!check_content){
str = "don't use (\" \') character in content";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
private void showProgressDialog(){
pd = new ProgressDialog(this);
pd.setIcon(R.drawable.ic_launcher);
pd.setTitle("กำลังทำงาน");
pd.setMessage("Downloading โปรดรอ...");
pd.setCancelable(true);
pd.show();
}
//implements TaskCallBack interface
private HttpPostTask.TaskCallBack mPostCallback_post = new HttpPostTask.TaskCallBack() {
@Override
public void onPreExecute() {
showProgressDialog();
}
@Override
public void onProgressUpdate(HttpPostTask.Response... values) {
HttpPostTask.Response res = values[0];
if (res.status != 200) {
return;
}
try {
JSONObject response = new JSONObject(res.data);
if (!response.getString("status").equals("success")) {
Log.d("onProgressUpdate","Fail");
return;
}
//JSONArray response = new JSONArray(res.data);
Log.d("onProgressUpdate",response.toString());//JSON
} catch (JSONException e) {
Log.d("onProgressUpdate", e.toString() + "");
}
}
@Override
public void onPostExecute(ArrayList<HttpPostTask.Response> result) {
pd.dismiss();
/*Log.d("onPostExecute_size" , String.valueOf(result.size()));
for(Response r : result){
Log.d("onPostExecute_Response_data" , r.data);//String
Log.d("onPostExecute_Response",r.toString());//Response
} */
result.clear();
}
@Override
public void onIOException() {
Log.d("onIOException","Fail");
}
};
//show dialog for choose date
@SuppressLint("ValidFragment")
public static class DatePickerFragment extends DialogFragment{
private int key;
public DatePickerFragment(){}
public DatePickerFragment(int k){
key = k;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//callback when set date
RTTDatePickerDialog.OnDateSetListener d = new RTTDatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
if(key == START_DATE){
start_date.setText(dayOfMonth+"/"+ (monthOfYear+1) +"/"+year);
end_date.setText("final date");
end_date.setTextColor(Color.BLACK);
end_date.setBackgroundDrawable(start_date.getBackground());
end_date.setClickable(true);
//start_flag = true;
start_year = year;
start_month = monthOfYear+1;
start_day = dayOfMonth;
end_year = 0;
end_month = 0;
end_day = 0;
end_flag = false;
}else if(key == END_DATE){
end_date.setText(dayOfMonth+"/"+ (monthOfYear+1) +"/"+year);
end_flag = true;
end_year = year;
end_month = monthOfYear+1;
end_day = dayOfMonth;
}
}
};
// Use the current date as the default date in the picker
if(key == START_DATE){
int year,month,day;
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
return new RTTDatePickerDialog(getActivity(), d, year, month, day);
}else if(key == END_DATE){
return new RTTDatePickerDialog(getActivity(), d, start_year, start_month-1, start_day);
}
return null;
}
}
private boolean isConnectInternet(){
ConnectivityManager connect = (ConnectivityManager)
getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connect != null){
NetworkInfo[] info = connect.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if(info[i].getState() == NetworkInfo.State.CONNECTED){
return true;
}
}
}
}
return false;
}
}
[head]นี่ code ฝั่ง serverครับ/head]
<?php
require("connect.php");
$Strfrom = mysql_escape_string($_POST['Strfrom']);
$Strcontent = mysql_escape_string($_POST['Strcontent']);
$Strstart_date = mysql_escape_string($_POST['Strstart_date']);
$Strend_date = mysql_escape_string($_POST['Strend_date']);
$date = date("Y-m-d H:i:s" , strtotime('now'));
$mes = "INSERT INTO activity(activity_id , when , content , start_date, end_date , from_user , status) VALUES(1, '$date' , '$Strcontent', '$Strstart_date', '$Strend_date', '$Strfrom', 1);";
$query = mysql_query($mes);
$returnArray=array("status"=>"success","mes"=>$mes,"result"=>$query);
echo json_encode($returnArray);
mysql_close($con);
?>
นี่ตัวอย่าง activity table ครับ
นี่ Log cat ครับ
Tag : Mobile, MySQL, Android, JAVA, Mobile
|
ประวัติการแก้ไข 2014-02-19 11:33:40 2014-02-19 11:44:58
|
|
|
|
|
Date :
2014-02-19 11:19:55 |
By :
pokneun |
View :
1573 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|