【问题标题】:Android JSON POST and then responseAndroid JSON POST 然后响应
【发布时间】:2016-04-19 18:37:58
【问题描述】:

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

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 有什么东西可以上网,我可以据此过滤数据

【问题讨论】:

标签: android json


【解决方案1】:

这是一个例子

JSONObject obj = new JSONObject();
obj.put("username", "YourUser");
HttpUrlConnectionJson.sendHTTPData("http://" + serverAddress + "/api/SomeUserMethod", obj);

HttpUrlConnectionJson 类

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();
            }
        }
    }
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2020-10-28
    • 1970-01-01
    • 2012-05-15
    • 2013-03-04
    相关资源
    最近更新 更多