Android的PHP连接错误? (添加Java code) [英] android php connection error? (add java code)

查看:195
本文介绍了Android的PHP连接错误? (添加Java code)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Android应用程序的位置(经度,纬度),并使用PHP然后10米半径范围内提取数据插入到数据库中。

问题是,当我测试使用的智能手机(本地测试OK)在code,数据插入不正确。表'usergps'有3列(名称,经纬度),我试code后,(0,0)插入。

您可以检查以下code?我加了Java code。

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_getgps);    公共无效的onClick(查看视图){
    GPS =新GpsActivity(getGpsValues​​.this);
    //这是新的零件
    意向意图= getIntent();
    //从MainActivity接收数据
    HashMap的<字符串,字符串>用户= db.getUserDetails();    字符串名称= user.get(名称);
    字符串email = user.get(电子邮件);    如果(gps.canGetLocation()){
        双纬度= gps.getLatitude();
        双LON = gps.getLongitude();        sendGps(纬度,经度,名);
    }    HashMap的<字符串,字符串> GPS = db.getNumbers();
    字符串NUM = gps.get(民);

}
}

 私人无效sendGps(最终双纬度,最终双LON,最终字符串名称){
  //用标签来取消该请求
  字符串tag_string_req =req_gps;  StringRequest strReq =新StringRequest(Method.POST,
          AppConfig.URL_GPS,新Response.Listener<串GT;(){              @覆盖
              公共无效onResponse(字符串响应){
                  Log.d(TAG,GPS回应:+ response.toString());
                  hideDialog();                  尝试{
                      JSONObject的jObj =新的JSONObject(响应);
                      布尔误差= jObj.getBoolean(错误);
                      如果(!错误){
                          // GPS成功地存储在MySQL中
                          //现在存储在sqlite的GPS的                          JSONObject的GPS = jObj.getJSONObject(GPS);
                          双纬度= gps.getDouble(纬度);
                          双LON = gps.getDouble(LON);
                          字符串名称= gps.getString(名称);                          //在用户表中插入一行。
                          db.addGps(纬度,经度,名);
                      }其他{                          //发生了注册错误。得到的错误
                          // 信息
                          串ERRORMSG = jObj.getString(ERROR_MSG);
                          Toast.makeText(getApplicationContext(),
                                  ERRORMSG,Toast.LENGTH_LONG).show();
                      }
                  }赶上(JSONException E){
                      e.printStackTrace();
                  }              }
          },新Response.ErrorListener(){              @覆盖
              公共无效onErrorResponse(VolleyError错误){
                  Log.e(TAG,注册错误:+ error.getMessage());
                  Toast.makeText(getApplicationContext(),
                          error.getMessage(),Toast.LENGTH_LONG).show();
                  hideDialog();
              }
          }){
  };  //添加请求,请求队列
  。AppController.getInstance()addToRequestQueue(strReq,tag_string_req);

}

这部分是从PHP获取数据。这是另一个类。

  / **
 *充分利用数据库​​中的用户数据
 * * /
公众的HashMap<字符串,字符串> getUserDetails(){
    HashMap的<字符串,字符串>用户=新的HashMap<字符串,字符串>();
    字符串selectQuery =SELECT * FROM+ TABLE_LOGIN;    SQLiteDatabase分贝= this.getReadableDatabase();
    光标光标= db.rawQuery(selectQuery,NULL);
    //移到第一行
    cursor.moveToFirst();
    如果(cursor.getCount()大于0){
        user.put(姓名,cursor.getString(1));
        user.put(电子邮件,cursor.getString(2));
        user.put(UID,cursor.getString(3));
        user.put(created_at,cursor.getString(4));
    }
    cursor.close();
    db.close();
    //回报用户
    Log.d(TAG,从SQLite的获取用户:+ user.toString());    返回用户;
}公众的HashMap<字符串,字符串> getNumbers(){
    HashMap的<字符串,字符串> GPS =新的HashMap<字符串,字符串>();
    字符串selectQuery =SELECT * FROM+ TABLE_GPS;    SQLiteDatabase分贝= this.getReadableDatabase();
    光标光标= db.rawQuery(selectQuery,NULL);
    //移到第一行
    cursor.moveToFirst();
    如果(cursor.getCount()大于0){
        gps.put(民,cursor.getString(1));
    }
    cursor.close();
    db.close();
    //回报用户
    Log.d(TAG,从SQLite的获取用户:+ gps.toString());    回复:GPS;
}

和PHP code是在这里。
Android的PHP连接错误? (+ MYSQL)

我不能在logcat中检查错误。我必须使用智能手机进行检查。
请检查code,让我知道。

 的HashMap<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
  params.put(纬度,+纬度);
  params.put(LON,+ LON);
  params.put(名,名);  JsonObjectRequest strReq =新JsonObjectRequest(Request.Method.POST,
          AppConfig.URL_GPS,新的JSONObject(PARAMS),新Response.Listener<&JSONObject的GT;(){              @覆盖
              公共无效onResponse(JSONObject的响应){
                  Log.d(TAG,GPS回应:+ response.toString());
                  hideDialog();         尝试{
              JSONObject的jObj =新的JSONObject();
              布尔误差= jObj.getBoolean(错误);
          如果(!错误){
                 JSONObject的GPS = jObj.getJSONObject(GPS);
                 字符串名称= gps.getString(名称);
                 双纬度= gps.getDouble(纬度);
                 双LON = gps.getDouble(LON);
          //在用户表中插入一行。
          db.addGps(名称,纬度,经度);
                        ...


解决方案

 私人无效sendGps(最终双纬度,最终双LON,最终字符串名称)

方法,您不发送的PARAMS(LAT LON名)到PHP后端。
我不那么熟悉PHP,但我想,因为这PARAMS并不包括在你的帖子,PHP使用默认值:为弦乐和0号

送您需要将您的请求的 StringRequest 的更改对数据的 JsonObjectRequest

所以你的code是这个样子:

 的HashMap<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(纬度,+纬度);
params.put(LON,+ LON);
params.put(名,名);JsonObjectRequest strReq =新JsonObjectRequest(
          AppConfig.URL_GPS,新的JSONObject(PARAMS),新Response.Listener<串GT;(){

注意你需要改变你的回应听众,因为你现在得到的JSON对象响应。

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);
                        ...

解决方案

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.

这篇关于Android的PHP连接错误? (添加Java code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆