我正在尝试使用 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);
?>