-2
我試圖將條碼掃描內容發佈到mysql數據庫。我不明白我的錯誤,當我發送沒有錯誤顯示,但仍然沒有發佈。最初顯示的PHP腳本,但現在顯示沒有action.Here的代碼。我真的被卡在這裏了。下面的活動是XyzActivity.java
和post1.php
,然後是activity_xyz.xml
。無法從Android應用程序發佈條碼掃描到MySQL數據庫
XyzActivity.java
package com.example.boss.classattendance;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.zxing.integration.android.IntentIntegrator;
import java.util.HashMap;
import java.util.Map;
import com.android.volley.toolbox.Volley;
public class XyzActivity extends AppCompatActivity implements
View.OnClickListener{
//private static final String TAG = "AbcActivity";
public static final String KEY_FORMAT = "scanFormat";
public static final String KEY_CONTENT = "scanContent";
private static final String URL_FOR_REGISTRATION =
"http://m.y.i.p/test/post1.php";
ProgressDialog progressDialog;
private TextView greetingTextView;
private Button scanBtn,post,Logout;
private TextView scan_format, scan_content;
String scanContent;
String scanFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xyz);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
Bundle bundle = getIntent().getExtras();
String user = bundle.getString("username");
greetingTextView = (TextView) findViewById(R.id.greeting_text_view);
greetingTextView.setText("Hello "+ user);
scanBtn = (Button)findViewById(R.id.scan_button);
scan_format = (TextView)findViewById(R.id.scan_format);
Logout=(Button)findViewById(R.id.logout_button);
scan_content = (TextView)findViewById(R.id.scan_content);
post=(Button)findViewById(R.id.post_button);
scanBtn.setOnClickListener(this);
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm();
}
});
Logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new
Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
public void onClick(View v){
if(v.getId()==R.id.scan_button){
com.google.zxing.integration.android.IntentIntegrator scanIntegrator
= new com.google.zxing.integration.android.IntentIntegrator(this);
scanIntegrator.initiateScan();
}}
public void onActivityResult(int requestCode, int resultCode, Intent
intent) {
com.google.zxing.integration.android.IntentResult scanningResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
scanContent = scanningResult.getContents();
scanFormat = scanningResult.getFormatName();
scan_format.setText("FORMAT: " + scanFormat);
scan_content.setText("CONTENT: " + scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
private void submitForm(){
final String scanContent = scan_content.getText().toString().trim();
final String scanFormat = scan_format.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
URL_FOR_REGISTRATION,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(XyzActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(XyzActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_CONTENT,scanContent);
params.put(KEY_FORMAT,scanFormat);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
post1.php
<?php
require_once 'android_login_connect.php';
$db = new android_login_connect();
$response = array("error" => FALSE);
if (isset($_POST['content']) && isset($_POST['format'])) {
$content = $_POST['content'];
$format = $_POST['format'];
$sql = "INSERT INTO post (content,format)
VALUES ('$KEY_CONTENT','$KEY_FORMAT')";
if(mysqli_query($con,$sql)){
echo ‘success’;
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
};
?>
activity_xyz.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scrollbars="vertical"
android:weightSum="1">
<TextView
android:id="@+id/greeting_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Hello, User"
android:textColor="@android:color/black"
android:textSize="30dp"
android:textStyle="bold"
android:layout_weight="0.09" />
<Button
android:id="@+id/scan_button"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:background="@color/login_button_background"
android:text="SCAN"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/scan_format"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_weight="0.09"
android:text="Format"
android:textColor="@android:color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/scan_content"
android:layout_width="match_parent"
android:layout_height="23dp"
android:layout_marginTop="40dp"
android:text="Content"
android:textColor="@android:color/black"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/post_button"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:background="@color/login_button_background"
android:text="POST"
android:textColor="@android:color/black"
android:textStyle="bold" />
<Button
android:id="@+id/logout_button"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="40dp"
android:background="@color/login_button_background"
android:text="Sign Out!!"
android:textColor="@android:color/black"
android:textStyle="bold" />
</LinearLayout>