0

我正在尝试使用 AsyncTask 类向后端服务器 php 文件发送 HTML 请求,但是,发送操作无法完成,logcat 中出现一些错误消息,我可以寻求一些解决问题的建议吗?

下面是我的 AsyncTask 类:

private class GrabURL extends AsyncTask<String, Void, Void>{

    ArrayList<NameValuePair> nameValuePairs;

    //private ProgressDialog Dialog;

    public GrabURL() { 
        nameValuePairs = new ArrayList<NameValuePair>(); 
        //Dialog = new ProgressDialog(Reader.this); 
    } 

    protected void onPreExecute(String key, String value) {
        mTest.append("PreExecute");
        //Dialog.setMessage("Sending value..");
        //Dialog.show();
        nameValuePairs.add(new BasicNameValuePair(key,value));
        }

    @Override
    protected Void doInBackground(String... urls) {
        // TODO Auto-generated method stub
        try{
            mTest.append("doinback");
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(urls[0]);
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
            post.setEntity(ent);
            client.execute(post); 
         } catch (Exception e) {
             e.printStackTrace();
         }
        return null;
    }

    protected void onPostExecute(Void unused) {
        mTest.append("PostExecute");
        //Dialog.dismiss();
        Toast.makeText(getApplicationContext(), "Value updated", Toast.LENGTH_SHORT).show();
    }
}

我通过在外部类中调用它来启动发送操作

grab.execute(new String[]{"http://192.168.1.150/myapp/check.php"});

如果我隐藏与对话框相关的那些内容,则不会显示任何错误消息,但是,它仍然无法与服务器通信。

如果我不隐藏它们,这里是错误消息

07-19 15:11:29.360: E/WindowManager(26555): Activity android.reader.Reader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416dddc8 that was originally added here
07-19 15:11:29.360: E/WindowManager(26555): android.view.WindowLeaked: Activity android.reader.Reader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416dddc8 that was originally added here
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
07-19 15:11:29.360: E/WindowManager(26555):     at android.app.Dialog.show(Dialog.java:278)
07-19 15:11:29.360: E/WindowManager(26555):     at android.reader.Reader$GrabURL.onPreExecute(Reader.java:228)
07-19 15:11:29.360: E/WindowManager(26555):     at android.reader.Reader$1.onClick(Reader.java:102)
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.View.performClick(View.java:3511)
07-19 15:11:29.360: E/WindowManager(26555):     at android.view.View$PerformClick.run(View.java:14105)
07-19 15:11:29.360: E/WindowManager(26555):     at android.os.Handler.handleCallback(Handler.java:605)
07-19 15:11:29.360: E/WindowManager(26555):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 15:11:29.360: E/WindowManager(26555):     at android.os.Looper.loop(Looper.java:137)
07-19 15:11:29.360: E/WindowManager(26555):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-19 15:11:29.360: E/WindowManager(26555):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 15:11:29.360: E/WindowManager(26555):     at java.lang.reflect.Method.invoke(Method.java:511)
07-19 15:11:29.360: E/WindowManager(26555):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-19 15:11:29.360: E/WindowManager(26555):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-19 15:11:29.360: E/WindowManager(26555):     at dalvik.system.NativeStart.main(Native Method)

下面是我的服务器端程序的代码,只是一个简单的 php 文件。

<?php
    $file = "C:/wamp/www/myapp/file.txt";
    $data = "AID:".$_POST['A']." BID:".$_POST['B'];
    file_put_contents($file, $data);
?>
4

3 回答 3

0

我已经检查了你的代码。它是您的服务器错误。我认为该职位无法正常工作。请检查它,然后将其放入 AsyncTask。

于 2012-07-19T05:02:22.297 回答
0

根据日志,执行停止在Dialog.show()in preExecute(),您不知道您的逻辑是否有任何问题,doInBackground()永远不会执行。

尝试这个:

  • 在构造函数中进行初始化
  • 确保Reader是您正在调用的活动的名称GrabURL

    private class GrabURL extends AsyncTask<String, Void, Void> {
    
    ArrayList<NameValuePair> nameValuePairs;
    private ProgressDialog Dialog;
    
    public void GrabURL() {
        nameValuePairs = new ArrayList<NameValuePair>();
        Dialog = new ProgressDialog(Reader.this);
    }
    
    protected void onPreExecute(String key, String value) {
        System.out.println("PreExecute");
        Dialog.setMessage("Sending value..");
        Dialog.show();
        nameValuePairs.add(new BasicNameValuePair(key, value));
    }
    
    @Override
    protected Void doInBackground(String... urls) {
        // TODO Auto-generated method stub
        try {
            System.out.println("doinback");
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(urls[0]);
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(
                    nameValuePairs, HTTP.UTF_8);
            post.setEntity(ent);
            client.execute(post);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    protected void onPostExecute(Void unused) {
        System.out.println("PostExecute");
        Dialog.dismiss();
        Toast.makeText(getApplicationContext(), "Value updated",
                Toast.LENGTH_SHORT).show();
    }
    }
    
于 2012-07-19T05:16:24.493 回答
0

问题似乎是您试图doInBackground()通过执行mTest.append("doinback");似乎是 TextView 来更新 UI。因此,删除它并更好地尝试从onPreExecute()和更新 UI 元素onPostExecute()

于 2012-07-19T06:55:56.887 回答