2016-04-19 44 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) 是最好的库外面来处理请求和响应。

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