为什么我应该在 httpPost() 之后重新启动 Wifi
难道你不知道为什么只能调用 onClickListener 一次,第二次尝试时我应该打开和关闭我的设备上的 Wifi 连接。第一次单击后,我无法浏览网页和阅读电子邮件 - 因此互联网连接被阻止。
Code:
package je.net.ua;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class App extends Activity {
Button button;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.Button01);
tv = (TextView) findViewById(R.id.TextView01);
Log.d("Dev", "Application started");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BufferedReader in;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://www.snee.com/xml/crud/posttest.cgi");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fname", "First name"));
postParameters.add(new BasicNameValuePair("lname", "Last name"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
Log.d("Dev", "Line = " + line);
sb.append(line + NL);
}
in.close();
tv.setText(line);
} catch (Exception ex) {
}
}
});
}
}
调试:
12-27 21:39:16.936: DEBUG/Dev(6568): Line = <html><head><title>posted data</title></head><body><h1>posted data</h1><p>First name: "First+name"</p><p>Last name: "Last+name"</p><p>REQUEST_URI: "/xml/crud/posttest.cgi"</p><p>QUERY_STRING: ""</p><p>CONTENT_LENGTH: "32"</p><p>content passed via STDIN: "fname=First+name&lname=Last+name"</p></body></html>
12-27 21:39:25.566: DEBUG/dalvikvm(85): GC_FOR_MALLOC freed 28784 objects / 1536696 bytes in 109ms
12-27 21:39:34.106: DEBUG/dalvikvm(6072): GC_EXPLICIT freed 1097 objects / 213648 bytes in 67ms
12-27 21:39:42.476: DEBUG/dalvikvm(5056): GC_EXPLICIT freed 1749 objects / 72040 bytes in 53ms
12-27 21:39:47.516: DEBUG/dalvikvm(29993): GC_EXPLICIT freed 1378 objects / 67232 bytes in 91ms
12-27 21:39:50.136: WARN/GTalkService(21165): [GTalkConnection.11] doConnect: caught XMPPError connecting to mtalk.google.com:5228.: (502)
12-27 21:39:50.136: WARN/GTalkService(21165): -- caused by: java.net.SocketException: The operation timed out
Don't you know why it is possible to invoke onClickListener only once, on second attempt I should turn on and turn off the Wifi connection on my device. After first click I can't browse web and read email - so internet connection is blocked.
Code:
package je.net.ua;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class App extends Activity {
Button button;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.Button01);
tv = (TextView) findViewById(R.id.TextView01);
Log.d("Dev", "Application started");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BufferedReader in;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://www.snee.com/xml/crud/posttest.cgi");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fname", "First name"));
postParameters.add(new BasicNameValuePair("lname", "Last name"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
Log.d("Dev", "Line = " + line);
sb.append(line + NL);
}
in.close();
tv.setText(line);
} catch (Exception ex) {
}
}
});
}
}
Debug:
12-27 21:39:16.936: DEBUG/Dev(6568): Line = <html><head><title>posted data</title></head><body><h1>posted data</h1><p>First name: "First+name"</p><p>Last name: "Last+name"</p><p>REQUEST_URI: "/xml/crud/posttest.cgi"</p><p>QUERY_STRING: ""</p><p>CONTENT_LENGTH: "32"</p><p>content passed via STDIN: "fname=First+name&lname=Last+name"</p></body></html>
12-27 21:39:25.566: DEBUG/dalvikvm(85): GC_FOR_MALLOC freed 28784 objects / 1536696 bytes in 109ms
12-27 21:39:34.106: DEBUG/dalvikvm(6072): GC_EXPLICIT freed 1097 objects / 213648 bytes in 67ms
12-27 21:39:42.476: DEBUG/dalvikvm(5056): GC_EXPLICIT freed 1749 objects / 72040 bytes in 53ms
12-27 21:39:47.516: DEBUG/dalvikvm(29993): GC_EXPLICIT freed 1378 objects / 67232 bytes in 91ms
12-27 21:39:50.136: WARN/GTalkService(21165): [GTalkConnection.11] doConnect: caught XMPPError connecting to mtalk.google.com:5228.: (502)
12-27 21:39:50.136: WARN/GTalkService(21165): -- caused by: java.net.SocketException: The operation timed out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来好像您的 OnClickListener 在等待
http://www.snee.com/xml/crud/posttest.cgi
的响应时被阻止。关闭 WiFi 会中断套接字连接,因此此时您的 OnClickListener 不再被阻止。如果您要将此处理程序的主要部分作为 AsyncTask 运行,我认为您会发现您可以多次单击该按钮(并在这样做时用 POST 淹没您的服务器;-)
更新
响应您的更新,尤其是网络和电子邮件被阻止:似乎有通过
DefaultHttpClient
发送 POST 还存在其他问题。我建议您考虑使用 AndroidHttpClient,如果您是针对 Froyo 及更高版本,并确保调用其 close() 方法。或者,如果您需要坚持使用DefaultHttpClient
因为您需要支持早期版本,请尝试在完成连接后调用client.getConnectionManager().shutdown()
。我不能保证这些步骤会有帮助,但无论如何,它们似乎都是值得采取的好步骤。(根据我的评论,如下:如果您在启动程序之前关闭 WiFi,会发生什么?它是否仍然阻止您浏览和发送电子邮件的能力?另外,您在调用
tv.setText(line 后是否看到返回的 HTML )
?最后,我建议将Log.d
调用放在异常处理程序内部和readLine()
循环之后,看看是否达到了其中一个.)It seems as if your OnClickListener is blocked while it waits for a response from
http://www.snee.com/xml/crud/posttest.cgi
. Turning the WiFi off breaks the socket connection, so your OnClickListener is no longer blocked at that point.If you were to run the main part of this handler as an AsyncTask, I think you'd find that you can click on the button any number of times (and deluge your server with POSTs as you do so ;-)
Update
Responding to your update, especially that web and email are blocked: It seems like there have been other issues with sending POSTs via
DefaultHttpClient
. I suggest you look into using AndroidHttpClient, if you're targeting Froyo and later releases, and be sure to invoke its close() method when you're finished with the connection. Or, if you need to stick withDefaultHttpClient
because you need to support earlier releases, try callingclient.getConnectionManager().shutdown()
when you're done with the connection. I can't promise that these steps would help, but they seem to be good steps to take regardless.(From my comment, below: What happens if you turn off WiFi before starting the program? Does it still block your ability to browse and send email? Also, are you seeing the returned HTML after you call
tv.setText(line)
? Lastly, I suggest puttingLog.d
calls inside of your exception handler and after thereadLine()
loop, and see if either of them are reached.)