Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android php连接错误?(添加java代码)_Java_Php_Android_Mysql - Fatal编程技术网

android php连接错误?(添加java代码)

android php连接错误?(添加java代码),java,php,android,mysql,Java,Php,Android,Mysql,我试图从android应用程序中获取位置(纬度、经度),并使用php将其插入数据库,然后提取10米半径内的数据 问题是,当我使用智能手机测试代码时(本地测试正常),数据没有正确插入。表“usergps”有3列(名称、纬度、经度),在我测试代码之后,(,0,0)被插入 你能查一下下面的代码吗?我添加了java代码 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS

我试图从android应用程序中获取位置(纬度、经度),并使用php将其插入数据库,然后提取10米半径内的数据

问题是,当我使用智能手机测试代码时(本地测试正常),数据没有正确插入。表“usergps”有3列(名称、纬度、经度),在我测试代码之后,(,0,0)被插入

你能查一下下面的代码吗?我添加了java代码

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

    public void onClick(View view) {
    gps = new GpsActivity(getGpsValues.this);
    //this is new part      
    Intent intent = getIntent();
    // Receiving the Data from MainActivity
    HashMap<String, String> user = db.getUserDetails();

    String name = user.get("name");
    String email = user.get("email");

    if(gps.canGetLocation()){
        double lat = gps.getLatitude();
        double lon = gps.getLongitude();

        sendGps(lat,lon,name);
    }    

    HashMap<String, String> gps = db.getNumbers();
    String num = gps.get("num");
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getgps);
公共void onClick(视图){
gps=新的GpsActivity(getGPsValue.this);
//这是新的部分
Intent=getIntent();
//从MainActivity接收数据
HashMap user=db.getUserDetails();
字符串名称=user.get(“名称”);
字符串email=user.get(“email”);
if(gps.canGetLocation()){
双纬度=gps.getLatitude();
double lon=gps.getLongitude();
sendGps(纬度、经度、名称);
}    
HashMap gps=db.getNumbers();
字符串num=gps.get(“num”);
} }

private void sendGps(最终双lat、最终双lon、最终字符串名){
//用于取消请求的标记
String tag\u String\u req=“req\u gps”;
StringRequest strReq=新的StringRequest(Method.POST,
AppConfig.URL\u GPS,新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(标记“GPS响应:”+Response.toString());
hideDialog();
试一试{
JSONObject jObj=新的JSONObject(响应);
布尔错误=jObj.getBoolean(“错误”);
如果(!错误){
//GPS成功存储在MySQL中
//现在将GPS存储在sqlite中
JSONObject gps=jObj.getJSONObject(“gps”);
双lat=gps.getDouble(“lat”);
Double lon=gps.getDouble(“lon”);
字符串名称=gps.getString(“名称”);
//在用户表中插入行。
db.addGps(纬度、经度、名称);
}否则{
//注册时出错。获取错误信息
//信息
String errorMsg=jObj.getString(“error_msg”);
Toast.makeText(getApplicationContext(),
errorMsg,Toast.LENGTH_LONG).show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(标记,“注册错误:+Error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(),Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
};
//将请求添加到请求队列
AppController.getInstance().addToRequestQueue(streq,标记字符串请求);
}

这部分是从php获取数据。这是另一门课

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        user.put("name", cursor.getString(1));
        user.put("email", cursor.getString(2));
        user.put("uid", cursor.getString(3));
        user.put("created_at", cursor.getString(4));
    }
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

    return user;
}

public HashMap<String, String> getNumbers() {
    HashMap<String, String> gps = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_GPS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        gps.put("num", cursor.getString(1));
    }
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + gps.toString());

    return gps;
}
/**
*从数据库获取用户数据
* */
公共HashMap getUserDetails(){
HashMap用户=新建HashMap();
String selectQuery=“SELECT*FROM”+表\u登录;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//移到第一排
cursor.moveToFirst();
if(cursor.getCount()>0){
user.put(“name”,cursor.getString(1));
user.put(“email”,cursor.getString(2));
user.put(“uid”,cursor.getString(3));
user.put(“created_at”,cursor.getString(4));
}
cursor.close();
db.close();
//返回用户
Log.d(标记,“从Sqlite获取用户:”+user.toString());
返回用户;
}
公共HashMap getNumbers(){
HashMap gps=新的HashMap();
String selectQuery=“SELECT*FROM”+表\u GPS;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//移到第一排
cursor.moveToFirst();
if(cursor.getCount()>0){
put(“num”,cursor.getString(1));
}
cursor.close();
db.close();
//返回用户
d(标记“从Sqlite获取用户:”+gps.toString());
返回gps;
}
PHP代码在这里。

我无法检查logcat中的错误。我得用智能手机检查一下。 请检查代码并让我知道

  HashMap<String, String> params = new HashMap<String, String>();
  params.put("lat", ""+lat);
  params.put("lon", ""+lon);
  params.put("name", name);

  JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST,
          AppConfig.URL_GPS, new JSONObject(params), new Response.Listener<JSONObject>() {

              @Override
              public void onResponse(JSONObject response) {
                  Log.d(TAG, "GPS Response: " + response.toString());
                  hideDialog();

         try {
              JSONObject jObj = new JSONObject();
              boolean error = jObj.getBoolean("error");
          if (!error) {
                 JSONObject gps = jObj.getJSONObject("gps");
                 String name = gps.getString("name");
                 Double lat = gps.getDouble("lat");
                 Double lon = gps.getDouble("lon");
          // Inserting row in users table.
          db.addGps(name, lat, lon);
                        ...
HashMap params=newhashmap();
参数put(“lat”,“lat+lat”);
参数put(“lon”和“+lon”);
参数put(“名称”,名称);
JsonObjectRequest strReq=新的JsonObjectRequest(Request.Method.POST,
AppConfig.URL_GPS,新的JSONObject(params),新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(标记“GPS响应:”+Response.toString());
hideDialog();
试一试{
JSONObject jObj=新的JSONObject();
布尔错误=jObj.getBoolean(“错误”);
如果(!错误){
JSONObject gps=jObj.getJSONObject(“gps”);
字符串名称=gps.getString(“名称”);
双纬度=gps.getDouble(“纬度”);
Double lon=gps.getDouble(“lon”);
//在用户表中插入行。
db.addGps(名称、纬度、经度);
...
在您的:

private void sendGps(final Double lat, final Double lon, final String name) 
方法,则不将参数(lat lon name)发送到PHP后端。 我对PHP不太熟悉,但我想,由于您的帖子中没有包含此参数,PHP使用默认值:“字符串”和0表示数字

要发送数据,您需要更改
private void sendGps(final Double lat, final Double lon, final String name) 
HashMap<String, String> params = new HashMap<String, String>();
params.put("lat", ""+lat);
params.put("lon", ""+lon);
params.put("name", name);

JsonObjectRequest strReq = new JsonObjectRequest(
          AppConfig.URL_GPS, new JSONObject(params), new Response.Listener<String>() {