[英]How to Send POST Json and and get result from the server (display as textview)?
[英]Get JSON value to TextView from HTTP post
我需要使用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 to 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.