Skip to content Skip to sidebar Skip to footer

I Am Trying Http Connection Over Android

I am a newbie on android development and i am having this error message while testing to send an sms over http connection and get a return string. below is the http api: http://ser

Solution 1:

In Android, you have to run network operations on a separate thread from the user interface. Otherwise, the act of sending the HTTP request causes the interface to freeze.

Android documentation

Solution 2:

Your code has a contradiction between the comment and the actual coding:

// This method sets the method type to POST so that// will be send as a POST request
        httpConnection.setRequestMethod("GET");

and then continues with setting both setDoInput and setDoOutput to true:

// This method is set as true wince we intend to send// input to the server
        httpConnection.setDoInput(true);
        // This method implies that we intend to receive data from server.
        httpConnection.setDoOutput(true);

The setDoOutput(true) implicitly set the request method to POST because that's the default method whenever you want to send a request body. (see HttpURLConnection sends a POST request even though httpCon.setRequestMethod("GET"); is set). This might cause an error.

For further research please indicate the error location (source line).

Solution 3:

I am glad to inform every one who showed concern to my problem that it has been fixed.

My code works fine and the Angad Tiwari's works and much simpler.

After i was able to get internet on my avd then i could use the printStackTrace() error report to find cause which was the Strict mode policy

the fix was for me to put this code below in the mainActivity class;

StrictMode.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

I appreciate you all.....:)

Solution 4:

rather using httpconnection i would suggest you to use httpclient like

try
            {
                HttpClient client=new DefaultHttpClient();
                HttpPost request=new HttpPost("your url");

                BasicNameValuePair email=new BasicNameValuePair("username", "your username");
                BasicNameValuePair password=new BasicNameValuePair("password", "your password");

                List<NameValuePair> list=new ArrayList<NameValuePair>();
                list.add(username);
                list.add(password);

                UrlEncodedFormEntity urlentity=new UrlEncodedFormEntity(list);
                request.setEntity(urlentity);

                HttpResponse response=client.execute(request);

                HttpEntity entity=response.getEntity();
                String tmp=EntityUtils.toString(entity);
                return tmp;
            }
            catch(Exception e)
            {
                return e.toString();
            }

try yourcode with this...and tell me ...i'll surelly work...and its also quite simple...there its all meshup with stream and all.. hope this will help... ;-)

Post a Comment for "I Am Trying Http Connection Over Android"