我有一個客戶端類,連接到服務器併發送POST請求,並作爲響應獲取Json答案。Android中的AsyncTask
public class ClientLocal extends AsyncTask<Void, Void, String> {
private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8"); //Media type for request
public JSONObject all_info_json;
private String page;
private SharedPreferences prefs;
private String login;
private String pass;
private StringBuilder request_body = new StringBuilder();
private OkHttpClient client = new OkHttpClient();
protected String doInBackground(Void... params) {
RequestBody body = RequestBody.create(JSON, request_body.toString());
Request request = new Request.Builder()
.url("https://program.yousystem.com.ua/frontend/api/user/login")
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
page = response.body().string();
//Log.i("PAGE", "page from try" + page);
return page;
} catch (IOException e) {
Log.i("ERROR", "1" + e.toString());
e.printStackTrace();
return null;
}
}
protected void onPostExecute(String page) {
try {
all_info_json = (JSONObject) new JSONTokener(page).nextValue();
JSONObject response = all_info_json.getJSONObject("response");
Log.i("PAGE", "RESP" + response.toString());
String token = response.getString("token");
Log.i("PAGE", "TOKEN " + token);
JSONObject profile = response.getJSONObject("profile");
Log.i("PAGE", "PROFILE" + profile.toString());
JSONArray balances = profile.getJSONArray("balances");
JSONObject balance_obj = balances.getJSONObject(0);
String balance = balance_obj.getString("balance");
Log.i("PAGE", "Balance" + balance);
JSONObject person = profile.getJSONObject("person");
String firstName = person.getString("firstName");
String lastName = person.getString("lastName");
String mobile = person.getString("mobile");
String email = person.getString("email");
} catch (JSONException e) {
e.printStackTrace();
}
// Log.i("PAGE", "page from post" + page);
}
ClientLocal(String login, String pass) {
this.login = login;
this.pass = pass;
this.request_body.append("{username: \"");
this.request_body.append(login);
this.request_body.append("\", pass: \"");
this.request_body.append(pass);
this.request_body.append("\", lng: \"ua\", prgCode: \"prg1\"}\"");
Log.i("", "page constructor" + request_body.toString());
}}
我需要使用AsyncTask在一個客戶端類中執行所有請求,任何人都可以幫助我,如何做到這一點?因爲要運行一個請求我使用ClientLocal client = new ClientLocal("login","pass");
之後client.execute();
在我看來,我需要創建Request builder類。但是如何在客戶端使用它呢?
public class RequestBuilder {
final private static String login_string = "https://program.yousystem.com.ua/frontend/api/user/login";
final private static String about_me = "https://program.yousystem.com.ua/frontend/api/user/me";
final private static String about_me_edit = "https://program.yousystem.com.ua/frontend/api/user/save";
final private static String transaction_list = "https://program.yousystem.com.ua/frontend/api/transaction/list";
String card_number;
String password;
StringBuilder request_body_login = new StringBuilder();
public RequestBuilder(String card_number_, String password_) {
this.card_number = card_number_;
this.password = password_;
}
public String LoginRequest(){
this.request_body_login.append("{username: \"");
this.request_body_login.append(card_number);
this.request_body_login.append("\", pass: \"");
this.request_body_login.append(password);
this.request_body_login.append("\", lng: \"ua\", prgCode: \"prg1\"}\"");
Log.i("", "page constructor" + request_body_login.toString());
return request_body_login.toString();
}}
登錄請求
Post https://program.yousystem.com.ua/frontend/api/user/login
Request {
"username": "username",
"pass": "pass",
"lng": "ua",
"prgCode": "prg1"
}
獲取資料信息
Post https://program.yousystem.com.ua/frontend/api/user/me
Request {
"prgCode": "prg1",
"token": "XT4PHNZBMSK73C7KH33NDJCN8A4SP5CJ"
}
交易清單
Post https://program.yousystem.com.ua/frontend/api/transaction/list
Request {
"withLimit": true,
"prgCode": "prg1",
"token": "XT4PHNZBMSK73C7KH33NDJCN8A4SP5CJ"
}
你想在一次執行所有的要求嗎? – kampangala
不在登錄活動登錄請求,在我的頁面活動獲取個人資料信息請求等。 –