Android使用xamp工作时通过php写入mysql

时间:2015-05-11 06:33:09

标签: php android mysql

我似乎无法理解为什么我的应用程序不断崩溃。我正在使用xamp localhosted服务器。我发布了3个文件,我的php文件,我的java android代码,以及我需要的xml。

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class createlocation extends Activity {

// Progress Dialog
private ProgressDialog pDialog;
JsonParser jsonParser = new JsonParser();
EditText LATITUDE;
EditText LONGITUDE;
EditText TIME;

// url to create new product
private static String location_entry_test =" 
 http://10.0.0.1/android_project/location_entry_test.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Edit Text
    LATITUDE = (EditText) findViewById(R.id.LATITUDE);
    LONGITUDE = (EditText) findViewById(R.id.LONGITUDE);
    TIME = (EditText) findViewById(R.id.TIME);

    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.btnCreateLOCATION);

    // button click event
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewlocation().execute();
        }
    });
  }

  /**
 * Background Async Task to Create new product
 * */
class CreateNewlocation extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(createlocation.this);
        pDialog.setMessage("Drone Located!..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String latitude = LATITUDE.getText().toString();
        String longitude = LONGITUDE.getText().toString();
        String time = TIME.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("latitude", latitude));
        params.add(new BasicNameValuePair("longitude", longitude));
        params.add(new BasicNameValuePair("time", time));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(location_entry_test,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product


                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
     }

  }
  }

---- PHP文件-----

<?php

/*
 * Following code will create a new product row
* All product details are read from HTTP Post Request
*/

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['latitude']) && isset($_POST['longitude']) &&  
                                                  isset($_POST['time'])) {

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$time = $_POST['time'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysqli_query("INSERT INTO products(latitude, longitude, Time)  

VALUES('$latitude', '$longitude', '$time')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "location has been successfully created.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "error occurred with query.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing..generlly going to be  
displayed when no android seciton is running";

// echoing JSON response
echo json_encode($response);
}
?>

--- xml文件---

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- LATITUDE -->
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="lATITUDE"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:textSize="17dip"/>

<!-- LATITUDE -->
<EditText android:id="@+id/LATITUDE"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_marginBottom="15dip"
    android:singleLine="true"
    android:inputType="numberDecimal"/>

<!-- LONGITUDE -->
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="LONGITUDE"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:textSize="17dip"/>

<!-- LONGITUDE -->
<EditText android:id="@+id/LONGITUDE"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_marginBottom="15dip"
    android:singleLine="true"
    android:inputType="numberDecimal"/>

<!-- TIME -->
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TIME"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:textSize="17dip"/>

<!-- TIME-->
<EditText android:id="@+id/TIME"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_marginBottom="15dip"
    android:lines="4"
    android:gravity="top"
    android:inputType="numberDecimal"/>

<!-- Button CreateS LOCATION -->
<Button android:id="@+id/btnCreateLOCATION"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Create LOCATION"/>

</LinearLayout>

---- ---- MySQL的

 location

Column  Type    Null    Default Links to    Comments
pid (Primary)   int(11) No           
latitude varchar(15)    No           
longitude   varchar(15) No           
time    decimal No           
Indexes

Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
PRIMARY BTREE   Yes No  pid 1   A   No  

Logcat结果(挑选)

05-14 00:11:01.649:E / AndroidRuntime(1905):致命异常:AsyncTask#1 05-14 00:11:01.649:E / AndroidRuntime(1905):进程:com.example.datagrabber,PID:1905 05-14 00:11:01.649:E / AndroidRuntime(1905):java.lang.RuntimeException:执行doInBackground()时发生错误 05-14 00:11:01.649:E / AndroidRuntime(1905):在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 05-14 00:11:01.649:E / AndroidRuntime(1905):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 05-14 00:11:01.649:E / AndroidRuntime(1905):at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 05-14 00:11:01.649:E / AndroidRuntime(1905):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 05-14 00:11:01.649:E / AndroidRuntime(1905):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 05-14 00:11:01.649:E / AndroidRuntime(1905):at java.lang.Thread.run(Thread.java:841) 05-14 00:11:01.649:E / AndroidRuntime(1905):引起:java.lang.NullPointerException 05-14 00:11:01.649:E / AndroidRuntime(1905):at com.example.datagrabber.createlocation $ CreateNewlocation.doInBackground(createlocation.java:97) 05-14 00:11:01.649:E / AndroidRuntime(1905):at com.example.datagrabber.createlocation $ CreateNewlocation.doInBackground(createlocation.java:97)

我没有机会从logcat添加它,但它也给了我appache错误无法连接......和

05-14 01:07:30.160:W / System.err(2283):引起:java.net.ConnectException:无法连接到/198.168.0.1(端口5554):连接失败:ETIMEDOUT(连接定时)出)

05-14 01:07:30.220:E / JSON Parser(2283):解析数据时出错org.json.JSONException:

字符0的输入结束

0 个答案:

没有答案