我正在尝试使用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);
?>
答案 0 :(得分:0)
我检查了你的代码。它是你的服务器错误。我认为这个帖子不是必须的。 请检查它,然后将其放入AsyncTask。
答案 1 :(得分:0)
根据日志,执行在Dialog.show()
的{{1}}停止,您不知道您的逻辑是否有任何问题,preExecute()
永远不会被执行。
试试这个:
确保doInBackground()
是您呼叫Reader
的活动的名称。
GrabURL
答案 2 :(得分:0)
问题似乎是您尝试通过执行似乎是TextView的doInBackground()
来更新mTest.append("doinback");
的UI。因此,请删除它,最好尝试更新onPreExecute()
和onPostExecute()
。