Json Post通过Android发布

时间:2016-04-19 16:57:29

标签: android json database

我知道如何使用android通过JSON解析数据,但我不知道如何通过它将一些数据发送到服务器,然后在此数据的基础上通过JSON检索数据到Android应用程序。这是将数据解析为android的代码:

private class GetFixture extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... arg) {


        ServiceHandler serviceClient = new ServiceHandler();
        Log.d("url: ", "> " + URL_ITEMS);
        String json = serviceClient.makeServiceCall(URL_ITEMS,ServiceHandler.GET);
        // print the json response in the log
        Log.d("Get match fixture resps","> " + json);
        if (json != null) {
            try {
                Log.d("try", "in the try");
                JSONObject jsonObj = new JSONObject(json);
                Log.d("jsonObject", "new json Object");
                // Getting JSON Array node
                matchFixture = jsonObj.getJSONArray(TAG_FIXTURE);
                Log.d("json aray", "user point array");
                int len = matchFixture.length();
                Log.d("len", "get array length");
                for (int i = 0; i < matchFixture.length(); i++) {
                    JSONObject c = matchFixture.getJSONObject(i);
                    String matchId = c.getString(TAG_MATCHID);
                    Log.d("matchId", matchId);
                    String teamA = c.getString(TAG_TEAMA);
                    Log.d("teamA", teamA);
                    String teamB = c.getString(TAG_TEAMB);
                    Log.d("teamB", teamB);
                    //  hashmap for single match
                    HashMap<String, String> matchFixture = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    matchFixture.put(TAG_MATCHID, matchId);
                    matchFixture.put(TAG_TEAMA, teamA);
                    matchFixture.put(TAG_TEAMB, teamB);
                    matchFixtureList.add(matchFixture);
                }
            }
            catch (JSONException e) {
                Log.d("catch", "in the catch");
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ListAdapter adapter = new SimpleAdapter(
                Doctor_Names.this, matchFixtureList,
                R.layout.list_item, new String[] {
                TAG_MATCHID, TAG_TEAMA,TAG_TEAMB
        }
                , new int[] {
                R.id.teamA,R.id.name,
                R.id.teamB
        }
        );
        setListAdapter(adapter);

    }





}

但是如何将数据发布到PHP然后做出一些决定呢?基本上,我有登录页面 - 它正确登录 - 但现在我想根据登录的人显示数据相关数据。为此,我必须将所需人员的用户名发送到服务器。我不知道该怎么做。

1 个答案:

答案 0 :(得分:-2)

OkHttp(tutorial) 和凌空(tutorial)  是处理请求和响应的最佳库。

您可以使用其中任何一个。它既简单又直接。