2016-04-19 45 views
0

我知道如何通過JSON解析數據使用android,但我不知道如何通過它發送一些數據到服務器,然後在此數據的基礎上通過JSON檢索數據到android應用程序。這是解析數據到Android的代碼:Json發佈通過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); 

    } 





} 

但是,我如何POST數據到PHP,然後做出一些決定?基本上,我有登錄頁面 - 登錄正確 - 但現在我想根據登錄的人顯示數據相關數據。爲此,我必須將所需人員的用戶名發送到服務器。我不知道該怎麼做。

+0

嘗試使用抽球...檢查[本教程](http://www.androidhive.info/2014/05/android-working-with-volley-library-1/) – anAmaka

+0

本網站是有用的你知道一些事情更好,我的最後一年的項目,我搜索很多,但什麼也沒有找到 –

+0

你需要使用HttpUrlConnection向Web服務器發出http請求。使用POST方法,並將轉換爲字符串的Json對象附加爲帖子的主體。 有各種各樣的庫,將爲您處理很多咕嚕的工作,但基本上他們會在後臺做什麼。 –

回答

-2

OkHttp(tutorial) 和截擊(tutorial) 是最好的庫外面來處理請求和響應。

您可以使用其中任何一個。這既簡單又直接。