【问题标题】:Android: Error connecting Android app with Database using php and jsonAndroid:使用 php 和 json 将 Android 应用程序与数据库连接时出错
【发布时间】:2018-06-30 21:57:39
【问题描述】:

我的 android 应用需要发送一个字符串,并基于该字符串从数据库中获取响应。 我有一个 php,它从应用程序接收字符串,查询数据库,并使用 echo json_encode($response_array); 返回响应,它在浏览器上运行良好并以 json 对象格式回显。

但是,在应用程序中,我使用的是 Volley。上面的 php 数组 $response_array 发送多个字符串,我需要在应用程序文本视图中显示这些字符串。

我已经在 gradle 依赖项中设置了 volley。

但是,从我的手机(连接到我的笔记本电脑热点)上运行应用程序时,我得到的错误是“null”。这是应用程序的示例代码。

    TextView o,t;
private RequestQueue requestQueue;
private static final String URL = "http://172.25.33.189/fadapp/mirror.php";
private StringRequest request;

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

    o=(TextView)findViewById(R.id.tryOne);
    t=(TextView)findViewById(R.id.tryTwo);

    requestQueue = Volley.newRequestQueue(this);
    request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonobject = new JSONObject(response);
                if (jsonobject.names().get(0).equals("name")) {
                    o.setText(jsonobject.getString("name"));
                    if(jsonobject.getString("stat").equals("1")) {
                        t.setText(R.string.inText);
                        t.setTextColor(Color.parseColor("#00FF00"));
                    } else {
                        t.setText(R.string.outText);
                        t.setTextColor(Color.parseColor("#FF0000"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();;
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("speid", "database query string here");
            return hashMap;
        }
    };
    requestQueue.add(request);

【问题讨论】:

    标签: java php android sql json


    【解决方案1】:

    您是否已通过 授予应用程序的互联网访问权限?

    如果您从笔记本电脑托管,您可能需要打开端口 80,以确保问题不是您的 Volley 设置尝试查询例如 google 并打印应该是 200 http 代码的响应

    【讨论】:

    • 不客气!如果您不想出现这些问题,请在路由器和防火墙上打开端口 80。禁用防火墙不是一个好主意
    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2015-05-20
    • 1970-01-01
    相关资源
    最近更新 更多