我想使用JSON将我的Android应用程序连接到Web服务

时间:2017-04-09 11:28:30

标签: android

我想将我的Android应用程序连接到Web服务,当我按下按钮时没有任何反应,Web服务正在运行并返回JSON ...而她是我的鳕鱼。

公共类MainActivity扩展了AppCompatActivity {

RequestQueue requestQueue ;
TextView textView;
Button start;



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

     start = (Button) findViewById(R.id.button);
     textView = (TextView) findViewById(R.id.textView3);
    requestQueue = Volley.newRequestQueue(this);



    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://192.168.5.160:8080/H2O.asmx/Menu_Get_Categories",

                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            try {
                                JSONArray jsonArray = response.getJSONArray("Categories");
                                for (int i = 0 ; i < jsonArray.length() ; i++){
                                    JSONObject Categories = jsonArray.getJSONObject(i);
                                    String Cat_AR_Name = Categories.getString("Cat_AR_Name");
                                    String Cat_EN_Name = Categories.getString("Cat_EN_Name");
                                    String CatID = Categories.getString("CatID");
                                    textView.append(Cat_AR_Name+" "+Cat_EN_Name+" "+CatID+" \n ");

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },

                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("VOLLEY","ERROR");

                        }
                    }
            );
            requestQueue.add(jsonObjectRequest);
        }

    });
}

}

1 个答案:

答案 0 :(得分:0)

我总是使用这个例子来使用Volley来解析JSON数据,但是我无法告诉你这是否会帮助你。

让我们说下面有Json数据:

[
    {
    "name" : "Alex Watson",
    "email" : "alex.watson@company.com",
    "phone" : {
        "home" : "0912345678",
        "mobile" : "0612345678"
    }
    },
    {
    "name" : "John Deep",
    "email" : "john.deep@company.com",
    "phone" : {
        "home" : "0912345678",
        "mobile" : "0612345678"
    }
    }
]

以下代码完成了这项工作:

JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());

                try {
                    // Parsing json array response
                    // loop through each json object
                    jsonResponse = "";
                    for (int i = 0; i < response.length(); i++) {

                        JSONObject person = (JSONObject) response
                                .get(i);

                        String name = person.getString("name");
                        String email = person.getString("email");
                        JSONObject phone = person
                                .getJSONObject("phone");
                        String home = phone.getString("home");
                        String mobile = phone.getString("mobile");

                        jsonResponse += "Name: " + name + "\n\n";
                        jsonResponse += "Email: " + email + "\n\n";
                        jsonResponse += "Home: " + home + "\n\n";
                        jsonResponse += "Mobile: " + mobile + "\n\n\n";

                    }

                    txtResponse.setText(jsonResponse);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

                hidepDialog();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();
            }
        });

// Adding request to request queue, please substitute here with your own queue
VolleyQueue.add(req);

您可以关注此tutorial