Doing HTTP POST with the current SDK

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: Doing HTTP POST with the current SDK

Postby ziptnf » Thu Nov 18, 2010 3:29 am

flintman, or anyone else who sees this, I have tried your code and am having trouble. I continually am getting IO Exceptions upon the execution of httpclient.execute(httppost);

The ports on the server I am posting to have been opened, ports 80, 21-25. Why would it refuse my connection?

Here is my code for reference:

Code: Select all
public HttpResponse doPost(String url, Map<String, String> kvPairs)       
           throws ClientProtocolException, IOException {
           HttpParams params = new BasicHttpParams();
            params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost(url);

           if (kvPairs != null && kvPairs.isEmpty() == false) {
              List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        kvPairs.size());
              String k, v;
              Iterator<String> itKeys = kvPairs.keySet().iterator();
              while (itKeys.hasNext()) {
                 k = itKeys.next();
                 v = kvPairs.get(k);
                 nameValuePairs.add(new BasicNameValuePair(k, v));
              }
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           }
           HttpResponse response;
           response = httpclient.execute(httppost);
           return response;
        }


Code: Select all
button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Map<String,String> vars = new HashMap<String,String>();
                HttpResponse response;
                String xml = null;
                vars.put("login", "admin");
                try {
                   response = doPost("http://myservername/android/login.php",vars);
                   InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    ByteArrayBuffer baf = new ByteArrayBuffer(20);

                     int current = 0; 
                     while((current = bis.read()) != -1){ 
                            baf.append((byte)current); 
                     } 
                     xml = EntityUtils.toString(response.getEntity());
                     Toast.makeText(v.getContext(), xml, Toast.LENGTH_LONG).show();
                } catch(ClientProtocolException cpe) {
                   Toast.makeText(v.getContext(), "There was a client issue. Try again later", Toast.LENGTH_LONG).show();
                } catch(IOException ioe) {
                   Toast.makeText(v.getContext(), "There was an IO issue. Try again later", Toast.LENGTH_LONG).show();
                   Toast.makeText(v.getContext(), ioe.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });


Here is the PHP code on the Apache server in login.php (which has full permissions):

Code: Select all
<?php echo $_POST['login']; ?>


It throws an IOException every time. Any idea why? Thanks for the help, people.
ziptnf
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 11, 2010 5:45 pm

Top

Re: Doing HTTP POST with the current SDK

Postby ziptnf » Thu Nov 18, 2010 4:29 am

I fixed it! The problem lies in extracting the Entity. You are only allowed to do this once.

http://groups.google.com/group/android- ... 60aa?pli=1

I removed the InputStream routine and it works.
ziptnf
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 11, 2010 5:45 pm

Re: Doing HTTP POST with the current SDK

Postby typ0 » Sun Nov 21, 2010 12:45 pm

When I call the Myfunction() by clicking a button it just pops up with this "Sorry!, The application AppName (process en.to.tre) has stopped unexpectedly. Please Try again." But why?
typ0
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Nov 21, 2010 11:51 am

Re: Doing HTTP POST with the current SDK

Postby squeeish » Wed Jun 22, 2011 7:26 am

postresponse.php is no longer up? I get a Not Found error when I post data to it.
squeeish
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jun 22, 2011 7:23 am

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests