将 HTTPost 发送到 URL 时 Android 应用程序挂在模拟器上?

发布于 2024-12-04 06:47:46 字数 1464 浏览 0 评论 0原文

我在android中编写了一段代码来发送一个url的httppost请求,而我使用像“http://xxxx/myfolder/values.aspx”这样的本地IP地址将数据发布到虚拟目录所在的本地服务器上是为values.aspx 创建的,下面编写的代码工作正常,并且可以轻松地将数据发送到本地主机。当在 httpost 请求中指定真实的 url 时,应用程序会挂起并且不会回复任何错误或异常。厌倦了没有结果的问题。请回复相关帖子。

=================================================== ===========================

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.xyz.com/myfolder/values.aspx");

    enter code here

try
 {
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
 nameValuePairs.add(new BasicNameValuePair("data",txtdis.getText().toString()
+txtsys.getText().toString()+txtpulse.getText().toString()+txttemp.getText().toString()));

 nameValuePairs.add(new BasicNameValuePair("reg", "asdfghjklpoiuytr")); 

  AbstractHttpEntity ent = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);       
  httppost.setEntity(ent);

  httppost.addHeader("data","vacant");
  httppost.addHeader("reg","vacant");
  HttpResponse response = httpclient.execute(httppost);

 if (ent != null) 
 { 
 Log.i("RESPONSE", response.getStatusLine().toString());
 }

 HttpEntity resEntity = response.getEntity();  
 if (resEntity != null) 
 {    
                                   Log.i("RESPONSE",EntityUtils.toString(resEntity));
 }


Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( "http://192.168.2.50/BAN_app/getvalues1.aspx" ) );

startActivity( browse );

I have wrriten a code in android to send a httppost request for a url, while i am using a local Ip address like "http://x.x.x.x/myfolder/values.aspx" to post the data on the local server where a virtual directory is created for values.aspx the code written below works fine, and sends the data to the localhost easily. While when a real url is specified in httpost request the application hangs and reply no error or exception. Fed up from the problem with no results. Please reply with a relevant POST.

=============================================================================

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.xyz.com/myfolder/values.aspx");

    enter code here

try
 {
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
 nameValuePairs.add(new BasicNameValuePair("data",txtdis.getText().toString()
+txtsys.getText().toString()+txtpulse.getText().toString()+txttemp.getText().toString()));

 nameValuePairs.add(new BasicNameValuePair("reg", "asdfghjklpoiuytr")); 

  AbstractHttpEntity ent = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);       
  httppost.setEntity(ent);

  httppost.addHeader("data","vacant");
  httppost.addHeader("reg","vacant");
  HttpResponse response = httpclient.execute(httppost);

 if (ent != null) 
 { 
 Log.i("RESPONSE", response.getStatusLine().toString());
 }

 HttpEntity resEntity = response.getEntity();  
 if (resEntity != null) 
 {    
                                   Log.i("RESPONSE",EntityUtils.toString(resEntity));
 }


Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( "http://192.168.2.50/BAN_app/getvalues1.aspx" ) );

startActivity( browse );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-12-11 06:47:46

您在 UI 线程中执行此请求。这会导致整个用户界面在请求期间被阻塞,并会触发 <如果您的请求花费的时间超过 5 秒,则会出现“应用程序未响应 (ANR)” 对话框。

始终在不同的线程中执行网络调用或工作量大的方法。 AsyncTask
类是完成这项工作的一个很棒且易于使用的工具。

You execute this request in the UI-thread. This results in blocking the whole ui for the time of the request, and will trigger an App Not Responding (ANR) dialog if your request takes longer than 5 seconds.

Always execute network calls or work-heavy methods in a different thread. The AsyncTask
class is a great and easy to use tool to get this job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文