gpt4 book ai didi

java - 如何使用带有 urlconnection 的 AsyncTask 来插入返回值?

转载 作者:行者123 更新时间:2023-12-02 10:22:24 25 4
gpt4 key购买 nike

我想使用 AsyncTask 与服务器连接,但出现错误 ondoInBackground。该错误需要 java.lang.string 那么如何改进 Update() 中的代码?如何在Update()方法中插入“Return”?

public void Update(final String urlString){
new Thread(new Runnable() {
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write("name=abc");
writer.close();

int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();

StringBuilder stringBuilder = new StringBuilder();
String line;

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
while ((line = br.readLine()) != null) {
stringBuilder .append(line);
}
String result = stringBuilder .toString();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

public class MyAsyncTask extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute(){
super.onPreExecute();
}

@Override
protected String doInBackground(String...url) {
return Update(url[0]);
}
@Override
protected void onPostExecute(String result){
Log.d("TAG", result);
}
}

最佳答案

How to use AsyncTask with urlconnection which requird to insert return

您需要在 Update() 方法中返回一个字符串

也不需要在 Update() 方法中使用另一个 Thread,因为 AsyncTask 已经在后台运行

示例代码

 public String Update(final String urlString){

String result="";

try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write("name=abc");
writer.close();

int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();

StringBuilder stringBuilder = new StringBuilder();
String line;

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
while ((line = br.readLine()) != null) {
stringBuilder .append(line);
}
result = stringBuilder .toString();
}
} catch (Exception e) {
e.printStackTrace();
}

return result;
}

public class MyAsyncTask extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute(){
super.onPreExecute();
}

@Override
protected String doInBackground(String...url) {
return Update(url[0]);
}
@Override
protected void onPostExecute(String result){
Log.d("TAG", result);
}
}

关于java - 如何使用带有 urlconnection 的 AsyncTask 来插入返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249091/

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