【问题标题】:Get JSON value to TextView from HTTP post从 HTTP 帖子获取 JSON 值到 TextView
【发布时间】:2018-11-24 03:52:13
【问题描述】:

我需要使用 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,但我不知道如何

【问题讨论】:

  • 你能用代码更新你的问题吗?显示你是如何调用getDatapostPHP的?
  • 好的,我已经更新了。
  • 使用runonUIthread设置数据。由于Textview在主线程上工作,而你的asynctask在后台线程中会抛出异常。

标签: java android json http


【解决方案1】:

如下更改您的代码:

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

【讨论】:

  • 我现在收到一个未处理的异常警告,用于返回 getData();部分。
  • @Roan 从您的方法中删除 throws ...。参考答案。
猜你喜欢
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 2019-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多