使用PHP传递JSON对象

时间:2018-11-03 12:01:00

标签: android json

我已经花了几个小时,现在我必须传递一个json对象,并在按下按钮时在显示屏上显示我不知道我要去哪里了,这里的代码是

Candidate

这是我的api文件,即时通讯用于获取本地主机获取api的即时消息,并使用xampp为我的php和apache服务器提供即时通讯

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

        username = (TextView) findViewById(R.id.textViewname);

        Button btnsubmit = (Button) findViewById(R.id.button);

        mQueue = Volley.newRequestQueue(this);

        btnsubmit.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View view)
            {
                Toast.makeText(getApplicationContext(),"button", Toast.LENGTH_SHORT).show();
                jsonParse();
            }
        });
    }


    private void jsonParse()
    {
        String url="http://localhost/api/get_data1.php";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                try {
                    response = response.getJSONObject("obj");

                    String first = response.getString("name");


                    username.setText(first);

                    Log.d("RESPONSE", response.toString());
                }
                catch (JSONException e1) {
                    e1.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError)
            {

            }
        });
    }

有人可以指出我的代码出了问题吗,无论如何,我可以在android studio上进行调试,就像在visual studio上调试

0 个答案:

没有答案