doutonghang2761 2015-06-05 08:30
浏览 54
已采纳

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

I'm trying to get location(latitude, longitude) from android application and insert into database using php then extract data within a 10m radius.

The Problem is, when I test the code using smartphone (local test is OK), data is not correctly inserted. Table 'usergps' has 3 columns (name, latitude, longitude) and after I test the code, ( , 0, 0) is inserted.

Can you check the code below? I added the java code.

@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");

} }

private void sendGps(final Double lat, final Double lon, final String name) {
  // Tag used to cancel the request
  String tag_string_req = "req_gps";

  StringRequest strReq = new StringRequest(Method.POST,
          AppConfig.URL_GPS, new Response.Listener<String>() {

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

                  try {
                      JSONObject jObj = new JSONObject(response);
                      boolean error = jObj.getBoolean("error");
                      if (!error) {
                          // GPS successfully stored in MySQL
                          // Now store the GPS in sqlite

                          JSONObject gps = jObj.getJSONObject("gps");
                          Double lat = gps.getDouble("lat");
                          Double lon = gps.getDouble("lon");
                          String name = gps.getString("name");

                          // Inserting row in users table.
                          db.addGps(lat, lon, name);
                      } else {

                          // Error occurred in registration. Get the error
                          // message
                          String errorMsg = jObj.getString("error_msg");
                          Toast.makeText(getApplicationContext(),
                                  errorMsg, Toast.LENGTH_LONG).show();
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }

              }
          }, new Response.ErrorListener() {

              @Override
              public void onErrorResponse(VolleyError error) {
                  Log.e(TAG, "Registration Error: " + error.getMessage());
                  Toast.makeText(getApplicationContext(),
                          error.getMessage(), Toast.LENGTH_LONG).show();
                  hideDialog();
              }
          }) {
  };

  // Adding request to request queue
  AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

this part is getting data from php. this is another class.

/**
 * 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;
}

and PHP code is here. Android php connection error? (+mysql)

I can't check error in logcat. I have to check this by using smartphone. Please check the code and let me know.

  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);
                        ...
  • 写回答

1条回答 默认 最新

  • dsstlsqt696435 2015-06-05 09:35
    关注

    in your:

    private void sendGps(final Double lat, final Double lon, final String name) 
    

    method, you don't send the params (lat lon name) to your PHP backend. I'm not so familiar with PHP, but I suppose, as this params are not included on your POST, php uses default values: "" for String and 0 for Numbers.

    to send data you need to change your Request from StringRequest to JsonObjectRequest

    so your code look something like this:

    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>() {
    

    be aware you need to change your response listener, because you get a json object response now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能