从HTTP帖子

时间:2018-06-14 11:18:46

标签: java android json http

我需要使用cookie执行HTTP帖子,并获取JSON响应并将其放入Android Studio中的TextView中。

到目前为止我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    new GetDataSync().execute();
    try {
        postPHP("Hoi");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

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


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

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}

private void postPHP (String cookie1) throws IOException, JSONException {

    CookieManager cookieManager = CookieManager.getInstance();
    String cookieString = cookieManager.getCookie(cookie1);
    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 void getData() throws IOException, JSONException {
    TextView txtUser = (TextView) findViewById(R.id.user);
    JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
    try {
        String response = json.getString("saldo");
        Log.e("saldo", response);
        response = json.getString("saldo");
        txtUser.setText(response);

    } catch (JSONException e) {

        e.printStackTrace();
    }
}

正如您所看到的,JSON到TextView和HTTP POST请求是两个独立的代码段,这就是为什么我没有获得正确的JSON值。我需要将两者结合起来,因此它首先通过POST发送cookie,然后将JSON响应放在TextView中,但我不知道如何

1 个答案:

答案 0 :(得分:0)

按如下方式更改您的代码:

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

        private final String cookie;

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


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

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

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

执行onCreate中的任务,如下所示:

new GetDataSync("Hoi").execute();