[英]How do I get the Value out of my Class into my TextViewField
At the moment I am trying to make an Android App with Android Studio with GET and POST data into/from an API.目前,我正在尝试使用 Android Studio 制作一个 Android 应用程序,其中包含从 API 获取和发布数据。
My problem at the moment is that I want to have the response of the POST request displayed in my app.我现在的问题是我想在我的应用程序中显示 POST 请求的响应。 For this I made the TextView TW_Rueckgabe.为此,我制作了 TextView TW_Rueckgabe。 I also made another Method just for this so it can Display the Request which is我还为此制作了另一种方法,因此它可以显示请求
public String returnString() {
return fetching_data;
}
but the response is not saved in TW_Rueckgabe.但响应未保存在 TW_Rueckgabe 中。
MainActivity.java MainActivity.java
final TextView[] TW_Rueckgabe = {findViewById(R.id.textViewRueckgabe)};
Button sendBtn = findViewById(R.id.sendBtn);
sendBtn.setOnClickListener(v -> {
String POST_url = "http://dphost.ddns.net:1573/cool/post.php";
String requestData = "data=" + TW_Benutzername.getText().toString();
POSTRequestTask test = (POSTRequestTask) new POSTRequestTask().execute(POST_url, requestData);
TW_Rueckgabe[0].setText(test.fetching_data);
});
POSTRequestTask Class: POSTRequestTask Class:
class POSTRequestTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String url = params[0];
String requestData = params[1];
String response = "";
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// add request header
con.setRequestMethod("POST");
con.setDoOutput(true);
// add request data
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(requestData);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer responseBuffer = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
responseBuffer.append(inputLine);
}
in.close();
response = responseBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
// print result
System.out.println("\n\n\n" + result + "\n\n\n");
fetching_data = result;
}
public String returnString() {
return fetching_data;
}
}
I tried to make a new Object and then save the Variable from the Object, didn't work.我试图制作一个新的 Object,然后从 Object 中保存变量,但没有用。
interface Callback {
void onResponse(String response);
}
MainActivity {
private Callback callback = new Callback() {
@Override
void onResponse(String response) {
TW_Rueckgabe[0].setText(test.fetching_data);
}
}
sendBtn.setOnClickListener(v -> {
String POST_url = "http://dphost.ddns.net:1573/cool/post.php";
String requestData = "data=" + TW_Benutzername.getText().toString();
POSTRequestTask test = new POSTRequestTask(callback).execute(POST_url, requestData);
});
}
class POSTRequestTask(Callback callback) extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// do your request as you're doing now and return the result
}
@Override
protected void onPostExecute(String result) {
// callback interface method call here
callback.onResponse(result)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.