gpt4 book ai didi

Java Android 将数据发送到服务器应用程序从不使用 onPostExecute()

转载 作者:搜寻专家 更新时间:2023-11-01 09:39:28 26 4
gpt4 key购买 nike

我在向服务器发送数据时遇到问题,为了连接我使用套接字的服务器。要将数据发送到服务器,我使用 AsyncTask。我有一个问题:

当应用程序发送数据时,我的应用程序看不到完成此操作,它从不使用 onPostExecute()

这是我的代码:

public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";

MyClientTask(String addr, int port) {
dstAddress = addr;
dstPort = port;
}

@Override
protected Void doInBackground(Void... arg0) {
// if (pingHost(1000)) {
socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

byte[] theByteArray = message.getBytes();
lengthMessage = (short) theByteArray.length;

outputStream.writeByte((byte) 0xB);
outputStream.writeByte((byte) 0xA);
outputStream.writeByte((byte) 0xA);
outputStream.writeByte((byte) 0xD);
outputStream.writeShort(lengthMessage);
outputStream.write(theByteArray);
outputStream.writeShort(width);
outputStream.writeShort(height);
outputStream.writeInt(lengthbmp);
outputStream.writeInt(lengthResizebmp);
outputStream.writeShort(11);
outputStream.write(imageInByte );
outputStream.write(imageResizeInByte);
outputStream.writeByte((byte) 0xB);
outputStream.writeByte((byte) 0xE);
outputStream.writeByte((byte) 0xE);
outputStream.writeByte((byte) 0xF);

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}


int bytesRead;
InputStream inputStream = socket.getInputStream();

while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}

outputStream.flush();
outputStream.close();

} catch (UnknownHostException e) {
e.printStackTrace();
isSuccsess = false;
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
Log.d("la", "nie udało sie");
isSuccsess = false;
response = "IOException: " + e.toString();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (socket != null) {
try {
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isSuccsess){
Toast.makeText(MainActivity.this, "Zdjęcie zostało wysłane !" , Toast.LENGTH_LONG).show();
bm = null;
clearEt();
ivImage.setImageBitmap(bm);
}
else{
Toast.makeText(MainActivity.this , "Nie udało się wysłać zdjęcia !" , Toast.LENGTH_LONG).show();
}

pbWheel.setVisibility(View.INVISIBLE);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pbWheel.setVisibility(View.VISIBLE);
}
}

最佳答案

您可以使用 AsyncTask<Void,Void,Integer> ,并在 doInBackground 中返回任何整数而不是 null。

只需替换(在 doInBackground 中)

return null;

return 1;

并替换(在 AsyncTask 声明中)

public class MyClientTask extends AsyncTask<Void, Void, Void> {

public class MyClientTask extends AsyncTask<Void, Void, Integer> {

关于Java Android 将数据发送到服务器应用程序从不使用 onPostExecute(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40716828/

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