首页 > 解决方案 > 在android中解析节点json

问题描述

我是安卓新手。我在nodejs中编写获取数据库表的请求代码。我用邮递员试试,它是正确的并返回json。但在 android studio 中,它在 android 监视器中返回空。下面是我的安卓代码。我的问题在哪里???

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_groups);
        get_groups();
    }


void get_groups() {


    final String url =  "http://192.168.215.2:8000/get_user2";

    final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.i("resp", response);

                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.i("err", error.getMessage());

                }
            }
    ) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new Hashtable<String, String>();
            map.put("name", Base64.encodeToString("".getBytes(), Base64.DEFAULT));
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(ActGroups.this);
    requestQueue.add(stringRequest);
}

这是邮递员 json:

 [
    {
        "id": 1,
        "u_name": "u",
        "u_family": "uu"
    },


    {
         "id": 3,
         "u_name": "a",
         "u_family": "aa"
    },

    {
        "id": 6,
        "u_name": "b",
        "u_family": "bb"
    }
]

以下是 logcat 输出:

05-25 12:57:25.641 1395-1395/? I/art: Not late-enabling -Xcheck:jni (already on)
05-25 12:57:25.641 1395-1395/? W/art: Unexpected CPU variant for X86 using defaults: x86
05-25 12:57:25.875 1395-1395/com.example.hosseinry.tour W/System: ClassLoader referenced unknown path: /data/app/com.example.hosseinry.tour-2/lib/x86
05-25 12:57:25.884 1395-1395/com.example.hosseinry.tour I/InstantRun: starting instant run server: is main process
05-25 12:57:26.018 1395-1395/com.example.hosseinry.tour W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-25 12:57:26.818 1395-1395/com.example.hosseinry.tour I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
05-25 12:57:26.846 1395-1443/com.example.hosseinry.tour D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-25 12:57:26.890 1395-1395/com.example.hosseinry.tour W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-25 12:57:27.078 1395-1433/com.example.hosseinry.tour I/OpenGLRenderer: Initialized EGL, version 1.4
05-25 12:57:27.078 1395-1433/com.example.hosseinry.tour D/OpenGLRenderer: Swap behavior 1
05-25 12:57:27.108 1395-1433/com.example.hosseinry.tour D/EGL_emulation: eglCreateContext: 0xa2bc6460: maj 2 min 0 rcv 2
05-25 12:57:27.160 1395-1433/com.example.hosseinry.tour D/EGL_emulation: eglMakeCurrent: 0xa2bc6460: ver 2 0 (tinfo 0x9fb92a00)
05-25 12:57:27.214 1395-1395/com.example.hosseinry.tour I/err: java.net.SocketException: Permission denied
05-25 12:57:27.217 1395-1433/com.example.hosseinry.tour D/EGL_emulation: eglMakeCurrent: 0xa2bc6460: ver 2 0 (tinfo 0x9fb92a00)

什么是解决方案?

标签: androidnode.js

解决方案


你得到一个错误:

java.net.SocketException:权限被拒绝

您可能没有正确处理权限,对于套接字,您必须添加:

<uses-permission android:name="android.permission.INTERNET"/>

到您的清单以允许您的应用访问互联网


推荐阅读