【发布时间】:2016-04-20 01:43:05
【问题描述】:
我知道如何使用 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,然后做出一些决定?基本上,我有登录页面 - 它已正确登录 - 但现在我想根据登录的人显示数据相关数据。为此,我必须将所需人员的用户名发送到服务器。我不知道该怎么做。
【问题讨论】:
-
尝试使用 volley...查看this tutorial
-
这个网站很有用,你知道一些更好的事情吗,它是我最后一年的项目,我搜索了很多,但一无所获
-
您需要使用 HttpUrlConnection 向网络服务器发出 http 请求。使用 POST 方法,并将转换为字符串的 Json 对象附加为帖子的正文。有各种库可以为您处理大量繁重的工作,但这基本上就是它们在后台所做的事情。
-
你能推荐我刚刚被困在这里的任何网站吗