通过Android发布Json
我知道如何使用android通过JSON解析数据,但我不知道如何通过它将一些数据发送到服务器,然后在此基础上通过JSON将数据检索到android应用程序。这是将数据解析到android的代码:通过Android发布Json,android,json,database,Android,Json,Database,我知道如何使用android通过JSON解析数据,但我不知道如何通过它将一些数据发送到服务器,然后在此基础上通过JSON将数据检索到android应用程序。这是将数据解析到android的代码: private class GetFixture extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute();
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);
}
}
私有类GetFixture扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…arg){
ServiceHandler serviceClient=新的ServiceHandler();
Log.d(“url:”,“>”+url\u项);
字符串json=serviceClient.makeServiceCall(URL\u项,ServiceHandler.GET);
//在日志中打印json响应
Log.d(“获取匹配夹具响应”,“>”+json);
if(json!=null){
试一试{
Log.d(“尝试”、“在尝试中”);
JSONObject jsonObj=新的JSONObject(json);
d(“jsonObject”,“新json对象”);
//获取JSON数组节点
matchFixture=jsonObj.getJSONArray(TAG_FIXTURE);
Log.d(“json aray”,“用户点数组”);
int len=matchFixture.length();
Log.d(“len”,“获取数组长度”);
对于(int i=0;ivalue
matchFixture.put(标记\u MATCHID,MATCHID);
匹配夹具。放置(标记为TEAMA,TEAMA);
匹配夹具。放置(标记为TEAMB,TEAMB);
matchFixtureList.add(matchFixture);
}
}
捕获(JSONException e){
Log.d(“捕获”、“捕获中”);
e、 printStackTrace();
}
}否则{
e(“JSON数据”,“没有从服务器接收任何数据!”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
ListAdapter=新的SimpleAdapter(
医生的名字,这个,matchFixtureList,
R.layout.list_项,新字符串[]{
标记匹配ID、标记团队A、标记团队B
}
,new int[]{
R.id.teamA,R.id.name,
R.id.teamB
}
);
setListAdapter(适配器);
}
}
但是我如何将数据发布到PHP,然后做出一些决定呢?基本上,我有一个登录页面——它已正确登录——但现在我想根据登录的人显示数据相关数据。为此,我必须将所需人员的用户名发送到服务器。我不知道怎么做。OkHttp()
和截击()
是处理请求和响应的最佳库
你可以用任何一个。它既简单又直观。尝试使用截击…查看此网站是否有用您是否知道一些更好的信息这是我的最后一年项目,我搜索了很多,但没有发现任何内容您需要使用HttpUrlConnection向Web服务器发出http请求。使用POST方法,并附加转换为字符串的Json对象作为POST的主体。有各种各样的图书馆可以为你们处理大量繁重的工作,但这基本上就是他们在后台所做的。你们能给我推荐一个网站吗?我只是被困在这里了