Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OnPostExecute()函数未在AsyncTask中执行_Android_Android Asynctask - Fatal编程技术网

Android OnPostExecute()函数未在AsyncTask中执行

Android OnPostExecute()函数未在AsyncTask中执行,android,android-asynctask,Android,Android Asynctask,您好,我正在开发一个在远程服务器上执行数据库查询的应用程序。到目前为止,我已经成功地与服务器端的PHP脚本进行了交互,并且我的代码发送字符串值没有问题,但是我不确定如何检查我是否通过PHP脚本接收了返回的值,因为我的OnPostExecute函数没有响应。 请帮忙 下面是面临问题的代码: public void submit_data(View V){ try{ new DoSocketProgramming().execute("10.0.2.2"); }

您好,我正在开发一个在远程服务器上执行数据库查询的应用程序。到目前为止,我已经成功地与服务器端的PHP脚本进行了交互,并且我的代码发送字符串值没有问题,但是我不确定如何检查我是否通过PHP脚本接收了返回的值,因为我的OnPostExecute函数没有响应。 请帮忙

下面是面临问题的代码:

public void submit_data(View V){
    try{
        new DoSocketProgramming().execute("10.0.2.2");
    }
    catch(Exception e){
        Toast.makeText(getApplicationContext(), "problem here", Toast.LENGTH_SHORT).show();
    }
    Toast.makeText(getApplicationContext(), "created till here", Toast.LENGTH_SHORT).show();
}

public class DoSocketProgramming extends AsyncTask<String, Void, String>
{
    String sendsentence=" this message is sent\n";
    String recvsentence=null;

    protected void onPreExecute()
    {
        Toast.makeText(getApplicationContext(), "this is preExecute", Toast.LENGTH_LONG).show();
    }
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        try{            

            Socket con=new Socket(addr,1678);
            DataInputStream dis=new DataInputStream(con.getInputStream());
            DataOutputStream dos=new DataOutputStream(con.getOutputStream());

            dos.writeUTF(sendsentence);
            recvsentence=dis.readUTF();

        }catch(Exception e){
            e.printStackTrace();
            }

        return recvsentence;
    }

    protected void onPostExecute(String result) {
        try{
        Toast.makeText(getApplicationContext(), "this is post execute"+ recvsentence, Toast.LENGTH_LONG).show();
        }catch(Exception e){
                e.printStackTrace();
        }
}       
public void提交数据(视图五){
试一试{
新的DoSocketProgramming()。执行(“10.0.2.2”);
}
捕获(例外e){
Toast.makeText(getApplicationContext(),“这里有问题”,Toast.LENGTH\u SHORT.show();
}
Toast.makeText(getApplicationContext(),“创建到这里”,Toast.LENGTH\u SHORT.show();
}
公共类DoSocketProgramming扩展了异步任务
{
String sendstension=“此消息已发送\n”;
字符串recvsentence=null;
受保护的void onPreExecute()
{
Toast.makeText(getApplicationContext(),“这是预先执行的”,Toast.LENGTH\u LONG.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
试试{
插座con=新插座(地址1678);
DataInputStream dis=新的DataInputStream(con.getInputStream());
DataOutputStream dos=新的DataOutputStream(con.getOutputStream());
dos.writeUTF(发送句子);
recvsentence=dis.readUTF();
}捕获(例外e){
e、 printStackTrace();
}
回执;
}
受保护的void onPostExecute(字符串结果){
试一试{
Toast.makeText(getApplicationContext(),“这是后期执行”+recvsentence,Toast.LENGTH\u LONG.show();
}捕获(例外e){
e、 printStackTrace();
}
}       

}实际上,这可能是真的。问题是您使用
getApplicationContext()
作为
上下文,这几乎总是错误的,除非您确切知道自己在做什么,否则不应该使用它

创建
AsyncTask
时,构造函数接收调用它的
活动的
上下文
,您应该将该
上下文
存储在
AsyncTask
类中,并在
onPostExecute()
方法中使用

这将是一个例子:

public class MyAsyncTask extends AsyncTask {
   Context context;

   private MyAsyncTask(Context context) { this.context = context; }

   @Override
   protected void onPostExecute(...) {
     super.onPostExecute(result);

     Toast.makeText(context, "this is post execute"+ recvsentence, Toast.LENGTH_LONG).show();
  }
}

尝试在
Try
语句之前放置一行
Log.d()。它是否显示在logcat中?只需输入如下内容:
Log.d(“MyApp”,“我已到达onPostExecute()”),当运行时,它应该在日志猫中显示一行这样的内容。请尝试将您的定义:
公共类DoSocketProgramming Extendes AsyncTask
替换为:
公共类DoSocketProgramming Extendes AsyncTask
仍然没有任何内容!。。我们可以有输入和输出流一起在那里!!!。。对吗?我试着把Log.d放在doinbackground()中,它可以正常工作。。。这意味着只有onPostExecute()不是。。。