Android Jingle
Android Jingle

Reputation: 1

Android Application hangs on emulator while sending HTTPost to a url?

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 );

Upvotes: 0

Views: 247

Answers (1)

user658042
user658042

Reputation:

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.

Upvotes: 2

Related Questions