发送POST到PHP并返回JSON

时间:2018-06-15 08:19:36

标签: java php android-studio cookies

所以我想将一个cookie发送到PHP服务器,我希望我已经拥有该部分代码的工作部分。但现在我想将返回的JSON值放在Android Studio的TextView中。而且我不知道如何让它发挥作用。

这是我到目前为止的代码:

public class MainActivity extends Activity {

    private SQLiteHandler db;
    private SessionManager session;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new GetDataSync("Hoi").execute();
        try {
            postPHP();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Button btnLogout = (Button) findViewById(R.id.btnLogout);

        // SqLite database handler
        db = new SQLiteHandler(getApplicationContext());

        // session manager
        session = new SessionManager(getApplicationContext());

        if (!session.isLoggedIn()) {
            logoutUser();
        }

        // Logout button click event
        btnLogout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                logoutUser();
            }
        });
    }

    public class GetDataSync extends AsyncTask<Void, Void, Void> {

        public final String cookie;

        public GetDataSync(String cookie) {
            this.cookie = cookie;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                postPHP();
                getData();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return;
        }

        protected void onPostExecute(String response) {
            super.onPostExecute(s);
            TextView txtUser = (TextView) findViewById(R.id.user);
            txtUser.setText(response);
        }
    }

    private String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    private void logoutUser() {
        session.setLogin(false);

        db.deleteUsers();

        // Launching the login activity
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void postPHP () throws IOException, JSONException {

        CookieManager cookieManager = CookieManager.getInstance();
        String cookieString = cookieManager.getCookie(cookie);
        URL url = new URL("http://piggybank.wordmediavormgever.nl/getSaldo.php");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("thatsallfolks", cookieString);
        connection.connect();
        OutputStream out = connection.getOutputStream();
        out.write(data);
        out.flush();
        out.close();
    }

    private String getData() throws JSONException, IOException {
        JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
        try {
            String response = json.getString("saldo");
            Log.e("saldo", response);
            response = json.getString("saldo");
            return response;

        } catch (Exception e) {

            return "";
        }
    }
}

如您所见,此代码还有一些问题。我无法让它正常工作。任何帮助表示赞赏!

0 个答案:

没有答案