gpt4 book ai didi

java - 从 HTTP post 获取 JSON 值到 TextView

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:00 26 4
gpt4 key购买 nike

我需要使用 cookie 发送 HTTP post,获取 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,但我不知道如何

最佳答案

如下更改您的代码:

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

关于java - 从 HTTP post 获取 JSON 值到 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com