package com.example.myapplication;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.ad.AdUtils;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity implements View.OnClickListener {
private EditText man_et, girl_et;
private HttpResponse<JsonNode> response;
private Button ok_bt;
private TextView fname, sname, percentage, result;
private String url;
private AdView adView;
private AdRequest adRequest;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdUtils.initBannaAd(getApplicationContext());
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_main);
initView();
countDownTimer.start();
}
private void initView() {
man_et = findViewById(R.id.man_et);
girl_et = findViewById(R.id.girl_et);
ok_bt = findViewById(R.id.ok_bt);
ok_bt.setOnClickListener(this);
fname = findViewById(R.id.fname);
sname = findViewById(R.id.sname);
percentage = findViewById(R.id.percentage);
result = findViewById(R.id.result);
adView = findViewById(R.id.ad_view);
progressBar = findViewById(R.id.progressBar);
getAdBanner();
}
private void getAdBanner() {
adRequest = new AdRequest.Builder()
.build();
adView.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int i) {
Log.w("linsheng", "load fail :" + i);
}
});
//加载请求
adView.loadAd(adRequest);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.ok_bt:
String manText = man_et.getText().toString().trim();
String girlText = girl_et.getText().toString().trim();
if (manText.equals("") || girlText.equals("")) {
Toast.makeText(getApplicationContext(), "Please fill out", Toast.LENGTH_LONG).show();
} else {
url = "https://love-calculator.p.rapidapi.com/getPercentage?fname=" + manText + "&sname=" + girlText;
new DownloadUrl().execute();
progressBar.setVisibility(View.VISIBLE);
}
getAdBanner();
AdUtils.showInterstitial();
break;
}
}
private CountDownTimer countDownTimer = new CountDownTimer(5200, 1000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
adView.loadAd(adRequest);
}
};
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
private class DownloadUrl extends AsyncTask<String, Void, HttpResponse<JsonNode>> {
@Override
protected HttpResponse<JsonNode> doInBackground(String... params) {
return getUrlData(url);
}
@Override
protected void onPostExecute(HttpResponse<JsonNode> response) {
progressBar.setVisibility(View.GONE);
JsonData(response);
}
}
private void JsonData(HttpResponse<JsonNode> response) {
try {
JSONObject jsonObject = new JSONObject(String.valueOf(response.getBody()));
String fnameString = jsonObject.getString("fname");
String snameString = jsonObject.getString("sname");
String percentageString = jsonObject.getString("percentage");
String resultString = jsonObject.getString("result");
fname.setText("fname:" + fnameString);
sname.setText("sname:" + snameString);
percentage.setText("percentage:" + percentageString);
result.setText("result:" + resultString);
if (percentageString.equals("100") || percentageString.equals("99")) {
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("linsheng", "JsonData: " + e.getMessage());
}
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
if (countDownTimer != null) {
countDownTimer.cancel();
}
}
private HttpResponse<JsonNode> getUrlData(String url) {
try {
response = Unirest.get(url)
.header("X-RapidAPI-Host", "love-calculator.p.rapidapi.com")
.header("X-RapidAPI-Key", "f9f933997dmsh2a210bfd2ae5d48p107477jsn9540cb2ef437")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
Log.e("linsheng", "onClick:aaaaaaaaaaaaa " + e.getMessage());
}
return response;
}
}
Android 请求数据返回解析
最新推荐文章于 2022-12-29 15:13:04 发布