我試圖從服務器獲取消息以顯示在吐司,但它沒有出現。客戶端接收來自服務器的消息,而無需任何errors.I曾試圖onPOST等打開UI線程,但它沒有工作吐司不在asynctask中顯示
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
new test().execute();
}
public class test extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... params) {
try
{
socket = new Socket("ip", port);
OutputStream outToServer = socket.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
Log.i(debugString, "Connected_reg!");
out.writeUTF("3");
InputStream inFromServer = socket.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
Log.i(debugString, in.readUTF());
string= in.readUTF();
}
catch (Exception e) {
Log.e(debugString, e.getMessage());
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
//super.onPostExecute(s);
Context context = getApplicationContext();
CharSequence text = string;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
好像你正在遭受'上下文'問題。也許[本文](https://possiblemobile.com/2013/06/context/)可以幫助您 – Pelocho