Application stopped unexpectedly - connecting to webpage

Put your problem here if it does not fit any of the other categories.

Application stopped unexpectedly - connecting to webpage

Postby NickStan » Wed Oct 14, 2009 5:00 pm

I am writing my first Android app.

I am trying to connect to a PHP page, pass some values to it and receive the values sent back.

The code I am using is some code that I found on Anndev, but its not working.

Here is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.example.WebService;
  5.  
  6.  
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import java.io.BufferedReader;
  13.  
  14. import java.io.IOException;
  15.  
  16. import java.io.InputStream;
  17.  
  18. import java.io.InputStreamReader;
  19.  
  20. import java.io.UnsupportedEncodingException;
  21.  
  22. import java.util.ArrayList;
  23.  
  24. import java.util.HashMap;
  25.  
  26. import java.util.Iterator;
  27.  
  28. import java.util.Map;
  29.  
  30. import java.util.Set;
  31.  
  32. import android.util.Log;
  33.  
  34.  
  35.  
  36. import org.apache.http.HttpEntity;
  37.  
  38. import org.apache.http.HttpResponse;
  39.  
  40. import org.apache.http.client.CookieStore;
  41.  
  42. import org.apache.http.client.entity.UrlEncodedFormEntity;
  43.  
  44. import org.apache.http.client.methods.HttpPost;
  45.  
  46. import org.apache.http.impl.client.DefaultHttpClient;
  47.  
  48. import org.apache.http.message.BasicNameValuePair;
  49.  
  50.  
  51.  
  52. import android.widget.TextView;
  53.  
  54.  
  55.  
  56. public class WebService extends Activity {
  57.  
  58.     private CookieStore cookies;
  59.  
  60.     private boolean isAuthenticated;
  61.  
  62.     private ArrayList<BasicNameValuePair> pairs;
  63.  
  64.     private DefaultHttpClient httpclient;
  65.  
  66.     private HttpPost httppost;
  67.  
  68.     private InputStream conteudo;
  69.  
  70.     private String retornoConexao;
  71.  
  72.        
  73.  
  74.     /** Called when the activity is first created. */
  75.  
  76.     @Override
  77.  
  78.     public void onCreate(Bundle savedInstanceState) {
  79.  
  80. //        super.onCreate(savedInstanceState);
  81.  
  82. //        setContentView(R.layout.main);
  83.  
  84.        
  85.  
  86.        
  87.  
  88.         //example of use
  89.  
  90.         Map<String,String> vars = new HashMap<String,String>();
  91.  
  92.         vars.put("developer_key", "XXXX");
  93.  
  94.         vars.put("uname", "test");
  95.  
  96.         vars.put("pw", "pwtest");
  97.  
  98.        
  99.  
  100.         parametrosHttp("http://777.777.777/authentication.php?", vars);
  101.  
  102.              
  103.  
  104.         go();
  105.  
  106.  
  107.  
  108.     }
  109.  
  110.    
  111.  
  112.     public void parametrosHttp(String url, Map<String,String> variables) {
  113.  
  114.         this.httpclient = new DefaultHttpClient();
  115.  
  116.         this.httppost = new HttpPost(url);
  117.  
  118.         this.pairs = new ArrayList();
  119.  
  120.         if(variables != null){
  121.  
  122.              Set keys = variables.keySet();
  123.  
  124.              for(Iterator i = keys.iterator(); i.hasNext();) {
  125.  
  126.                   String key = (String)i.next();
  127.  
  128.                   pairs.add(new BasicNameValuePair(key, variables.get(key)));
  129.  
  130.              }
  131.  
  132.         }
  133.  
  134.    }
  135.  
  136.    
  137.  
  138.    public String go(){
  139.  
  140.         try {
  141.  
  142.             UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "ISO-8859-1");
  143.  
  144.          
  145.  
  146.             /** Assign the POST data to the entity */
  147.  
  148.             httppost.setEntity(p_entity);
  149.  
  150.          
  151.  
  152.             /** Perform the actual HTTP POST */
  153.  
  154.             HttpResponse response = httpclient.execute(httppost);
  155.  
  156.             HttpEntity entity = response.getEntity();
  157.  
  158.             conteudo = entity.getContent();
  159.  
  160.             this.retornoConexao = convertStreamToString(conteudo);
  161.  
  162.             Log.d("HttpPostConnection",">>>>>>>>>>>>>>> "+retornoConexao);
  163.  
  164.             int status_code = response.getStatusLine().getStatusCode();
  165.  
  166.             this.cookies = httpclient.getCookieStore();
  167.  
  168.             if (status_code >= 300) {
  169.  
  170.                 this.isAuthenticated = false;
  171.  
  172.             } else {
  173.  
  174.                 this.isAuthenticated = true;
  175.  
  176.             }
  177.  
  178.         } catch (UnsupportedEncodingException uee) {
  179.  
  180.             // Woops
  181.  
  182.         } catch (IOException ioe) {
  183.  
  184.             // Woops
  185.  
  186.         } catch(IllegalStateException ise) {
  187.  
  188.              // woops
  189.  
  190.         }
  191.  
  192.         return retornoConexao;
  193.  
  194.    }
  195.  
  196.    
  197.  
  198.    public String convertStreamToString(InputStream is) {
  199.  
  200.         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  201.  
  202.         StringBuilder sb = new StringBuilder();
  203.  
  204.  
  205.  
  206.         String line = null;
  207.  
  208.         try {
  209.  
  210.              while ((line = reader.readLine()) != null) {
  211.  
  212.                   sb.append(line + "\n");                  
  213.  
  214.              }
  215.  
  216.         } catch (IOException e) {
  217.  
  218.              e.printStackTrace();
  219.  
  220.         } finally {
  221.  
  222.              try {
  223.  
  224.                   is.close();
  225.  
  226.              } catch (IOException e) {
  227.  
  228.                   e.printStackTrace();
  229.  
  230.              }
  231.  
  232.         }
  233.  
  234.  
  235.  
  236.         return sb.toString();
  237.  
  238.    }
  239.  
  240.  
  241.  
  242.    public boolean isAuthenticated() {
  243.  
  244.         return isAuthenticated;
  245.  
  246.    }    
  247.  
  248. }
  249.  
  250.  
  251.  
  252.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
Dont resist the inevitable
NickStan
Freshman
Freshman
 
Posts: 7
Joined: Mon Oct 12, 2009 4:54 pm

Top

connecting to webpage - IOException

Postby NickStan » Thu Oct 15, 2009 10:18 am

Hi Guys

Can someone please help?

I have changed my app (see code below) - the error I am getting now is IOException.

I have also added the following line to my manifest file:

<uses-permission android:name="android.permission.INTERNET" />

Any help appreciated.

Thanks

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.example.WebService;
  5.  
  6.  
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import java.io.BufferedReader;
  13.  
  14. import java.io.IOException;
  15.  
  16. import java.io.InputStream;
  17.  
  18. import java.io.InputStreamReader;
  19.  
  20. import java.io.UnsupportedEncodingException;
  21.  
  22. import java.util.ArrayList;
  23.  
  24. import java.util.HashMap;
  25.  
  26. import java.util.Iterator;
  27.  
  28. import java.util.Map;
  29.  
  30. import java.util.Set;
  31.  
  32. import java.net.HttpURLConnection;
  33.  
  34. import java.net.URL;
  35.  
  36. import java.net.URLConnection;
  37.  
  38.  
  39.  
  40. import android.util.Log;
  41.  
  42.  
  43.  
  44. import org.apache.http.HttpEntity;
  45.  
  46. import org.apache.http.HttpResponse;
  47.  
  48. import org.apache.http.client.methods.*;
  49.  
  50. import org.apache.http.client.CookieStore;
  51.  
  52. import org.apache.http.client.entity.UrlEncodedFormEntity;
  53.  
  54. import org.apache.http.client.methods.HttpPost;
  55.  
  56. import org.apache.http.impl.client.DefaultHttpClient;
  57.  
  58. import org.apache.http.message.BasicNameValuePair;
  59.  
  60.  
  61.  
  62. import android.widget.TextView;
  63.  
  64.  
  65.  
  66. public class WebService extends Activity {
  67.  
  68.     private CookieStore cookies;
  69.  
  70.     private boolean isAuthenticated;
  71.  
  72.     private ArrayList<BasicNameValuePair> pairs;
  73.  
  74.     private DefaultHttpClient httpclient;
  75.  
  76.     private HttpPost httppost;
  77.  
  78.     private InputStream conteudo;
  79.  
  80.     private String retornoConexao;
  81.  
  82.        
  83.  
  84.     /** Called when the activity is first created. */
  85.  
  86.     @Override
  87.  
  88.     public void onCreate(Bundle savedInstanceState) {
  89.  
  90. //        super.onCreate(savedInstanceState);
  91.  
  92. //        setContentView(R.layout.main);
  93.  
  94.  
  95.  
  96.         super.onCreate(savedInstanceState);
  97.  
  98.         // We want to view some very simple text, so we need a TextView
  99.  
  100.         TextView tv = new TextView(this);
  101.  
  102.         // Put some text to the newly created TextVIew
  103.  
  104.        
  105.  
  106.         tv.setText("Before connection");
  107.  
  108.        
  109.  
  110.         // Tell our App to display the textView
  111.  
  112.         this.setContentView(tv);          
  113.  
  114.        
  115.  
  116.        
  117.  
  118.         try {
  119.  
  120.             URL yahoo = new URL("http://777.777.777/authentication.php?developer_key=XXXX&uname=test&pw=pwtest");
  121.  
  122.             URLConnection yc = yahoo.openConnection();
  123.  
  124.            
  125.  
  126.             tv.setText("After connection");
  127.  
  128.            
  129.  
  130.             // Tell our App to display the textView
  131.  
  132.             this.setContentView(tv);          
  133.  
  134.            
  135.  
  136.            
  137.  
  138.             BufferedReader in = new BufferedReader(
  139.  
  140.                                     new InputStreamReader(
  141.  
  142.                                     yc.getInputStream()));
  143.  
  144.            
  145.  
  146.             String inputLine;
  147.  
  148.  
  149.  
  150.             while ((inputLine = in.readLine()) != null)
  151.  
  152. //                System.out.println(inputLine);
  153.  
  154.            
  155.  
  156.  
  157.  
  158.             in.close();
  159.  
  160.                 }
  161.  
  162.             catch(IOException ioe)
  163.  
  164.             {
  165.  
  166.                 //ioe.printStackTrace();
  167.  
  168.                 tv.setText("IOException");
  169.  
  170.                
  171.  
  172.                 // Tell our App to display the textView
  173.  
  174.                 this.setContentView(tv);                          
  175.  
  176.                
  177.  
  178.              }
  179.  
  180.             catch(Exception e)
  181.  
  182.             {
  183.  
  184.                //System.err.println("General Exception " + e);
  185.  
  186.                //e.printStackTrace();
  187.  
  188.                
  189.  
  190.                tv.setText("General exception");
  191.  
  192.                
  193.  
  194.                // Tell our App to display the textView
  195.  
  196.                this.setContentView(tv);          
  197.  
  198.  
  199.  
  200.              }
  201.  
  202.  
  203.  
  204. }
  205.  
  206.  
  207.  
  208.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
Dont resist the inevitable
NickStan
Freshman
Freshman
 
Posts: 7
Joined: Mon Oct 12, 2009 4:54 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 9 guests