doutonghang2761 2015-06-05 00: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 01: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 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部