首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android发布,然后响应

Android发布,然后响应
EN

Stack Overflow用户
提问于 2016-04-19 18:37:58
回答 1查看 1.5K关注 0票数 0

我可以通过JSONlistview中检索数据,但是我希望显示与登录的用户相关的数据,因此我必须将用户名发布到PHP脚本。我不知道如何将用户名发布到PHP脚本,然后从web服务器获得响应。

代码语言:javascript
运行
复制
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脚本,这样我就不会想到POST任何可以过滤数据的东西了。

EN

回答 1

Stack Overflow用户

发布于 2016-04-19 19:02:15

这里是一个示例

代码语言:javascript
运行
复制
JSONObject obj = new JSONObject();
obj.put("username", "YourUser");
HttpUrlConnectionJson.sendHTTPData("http://" + serverAddress + "/api/SomeUserMethod", obj);

HttpUrlConnectionJson类

代码语言:javascript
运行
复制
public class HttpUrlConnectionJson {

    private static final String TAG = "HttpUrlConnectionJson";

    public static String sendHTTPData(String urlpath, JSONObject json) {
        HttpURLConnection connection = null;
        try {
            URL url=new URL(urlpath);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
            OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream());
            streamWriter.write(json.toString());
            streamWriter.flush();
            StringBuilder stringBuilder = new StringBuilder();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(streamReader);
                String response = null;
                while ((response = bufferedReader.readLine()) != null) {
                    stringBuilder.append(response + "\n");
                }
                bufferedReader.close();

                Log.d(TAG, stringBuilder.toString());
                return stringBuilder.toString();
            } else {
                Log.e(TAG, connection.getResponseMessage());
                return null;
            }
        } catch (Exception exception){
            Log.e(TAG, exception.toString());
            return null;
        } finally {
            if (connection != null){
                connection.disconnect();
            }
        }
    }
}

我希望这能帮上忙

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36726784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档