내 앱에는 버튼을 몇 개 가지고 있는데, 이것을 클릭하면 데이터가 mysql 데이터베이스에 저장되고 작동한다. 문제는 10-15 페이지가있어서 데이터를 저장해야하기 때문에 10-15 PHP 페이지 대신 mysql에 데이터를 저장하는 대신 하나를 사용하고 싶습니다.버튼에서 PHP에서의 isset 포스트에 이르기까지
이 일 것입니다 내 버튼
mSubmit = (Button)findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
내 PHP
if (!empty($_POST)) {
//initial query
$query = "INSERT INTO comments (username, title, message) VALUES (:user, :title, :message) ";
//Update query
$query_params = array(
':user' => $_POST['username'],
':title' => $_POST['title'],
':message' => $_POST['message']
);
내 자바
package overskov.rhkbrand;
@SuppressLint("SimpleDateFormat")
public class AddComment extends Fragment {
private EditText lys, skilt, door, etDate;
private Button mSubmit;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String POST_COMMENT_URL = "http://xxx.xxx.x.x:1234/webservice/addcomment.php";
//testing on Emulator:
private static final String POST_COMMENT_URL = "http://overskov-hansen.dk/addcomment.php";
//testing from a real server:
//private static final String POST_COMMENT_URL = "http://www.mybringback.com/webservice/addcomment.php";
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.opgang_a, container, false);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.add_comment);
lys =(EditText) rootView.findViewById(R.id.lys);
skilt = (EditText)rootView.findViewById(R.id.skilt);
door = (EditText)rootView.findViewById(R.id.door);
etDate = (EditText)rootView.findViewById(R.id.etDate);
//Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
etDate.setText(format.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
mSubmit = (Button)rootView.findViewById(R.id.under_a);
mSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
PostComment mTask = new PostComment(getActivity());
mTask.execute();
//new PostComment().execute();
}
});
return rootView;
}
//////////////////////
class PostComment extends AsyncTask<String, String, String> {
private Context context;
ProgressDialog prog;
public PostComment(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Posting Comment...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String post_lys = lys.getText().toString();
String post_skilt = skilt.getText().toString();
String post_door = door.getText().toString();
String post_etDate = etDate.getText().toString();
//We need to change this:
String post_username = "admin";
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", post_username));
params.add(new BasicNameValuePair("lys", post_lys));
params.add(new BasicNameValuePair("skilt", post_skilt));
params.add(new BasicNameValuePair("door", post_door));
params.add(new BasicNameValuePair("etDate", post_etDate));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json;
json = jsonParser.makeHttpRequest(
POST_COMMENT_URL, "POST", params);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
//finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(getActivity(), file_url, Toast.LENGTH_LONG).show();
}
}
}
}
내 PHP
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (isset($_POST['under_a'])) {
//initial query
$query = "INSERT INTO under_a (username, lys, skilt, door, etDate) VALUES (:user, :lys, :skilt, :door, :etDate) ";
//Update query
$query_params = array(
':user' => $_POST['username'],
':lys' => $_POST['lys'],
':skilt' => $_POST['skilt'],
':door' => $_POST['door'],
':etDate' => $_POST['etDate']
);
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add post!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Post Successfully Added!";
echo json_encode($response);
}
?>
내 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="35dp"
android:background="#000000"
android:paddingLeft="10dp"
android:text="@string/opgang_a"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<EditText
android:id="@+id/etDate"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/edit_text_design"
android:clickable="false"
android:cursorVisible="false"
android:ems="15"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical|right"
android:inputType="date"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="30dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="#000000"
android:text="@string/lys" />
<EditText
android:id="@+id/lys"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:text="@string/ok"
android:textSize="14sp"
android:inputType="text"/>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginTop="20dp"
android:background="#000000"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="@string/skilte"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<EditText
android:id="@+id/skilt"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:text="@string/ok"
android:textSize="14sp"
android:inputType="text" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginTop="20dp"
android:background="#000000"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="@string/door"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<EditText
android:id="@+id/door"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:inputType="text"
android:text="@string/ok"
android:textSize="14sp" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/under_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#000000"
android:text="@string/gem_data"
android:textColor="#FFFFFF" />
</RelativeLayout>
내 로그 캣
02-20 20:51:09.317: E/JSON Parser(19779): Error parsing data org.json.JSONException: End of input at character 2 of
02-20 20 : 51 : 09.322 : W/dalvikvm (19779) : threadid = 13 : (group = 0x41ad2700) 02-20 20 : 51 : 09.332 : E/AndroidRuntime (19779) : 비동기 예외 : AsyncTask # 1 02-20 20 : 51 : 09.332 : E/AndroidRuntime (19779) : java.lang.RuntimeException : doInBackground() 실행 중 오류가 발생했습니다. 02-20 20 : 51 : 09.332 : E/AndroidRuntime (19779) : android.os.AsyncTask $ 3.done (AsyncTask.java:299) 02- 20 20 : 51 : 09.332 : E/AndroidRuntime (19779) :에서 java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:352)
더 많은 코드를 표시하여 변수의 출처를 확인할 수 있습니다. 또한 $ _POST [ 'name of item']은 item의 id가 아닙니다. –
게시물 이름을 변경하지 마십시오 PHP에서 – user3272367