Emulator Crashes when connecting to web service.

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Emulator Crashes when connecting to web service.

Postby PhilKearney » Fri Apr 22, 2011 5:50 pm

Hi Guys, I have a local REST web service set up which is connected to a mySQL database. This web service was created in netbeans and produces JSON. I have a bit of code that I wish to connect to the web service and when I click a button display in a TOAST the hotels name from the http request. The URL which I give it links to well formed JSON. Here is my main class. This is from a Tutorial pretty much and I tweaked to what I think would work.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public class Main extends Activity
  2. {
  3.  
  4.     private static final String url = "http://10.0.2.2:8080/hotelDB/resources/entities.hoteltable";
  5.     protected InitTask _initTask;
  6.  
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState)
  9.     {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.main);
  12.  
  13.         Button button = (Button)findViewById(R.id.button1);
  14.         button.setOnClickListener(new View.OnClickListener()
  15.         {
  16.             public void onClick(View v)
  17.             {
  18.                 _initTask = new InitTask();
  19.                 _initTask.execute( getApplicationContext() );
  20.             }
  21.         });
  22.     }
  23.  
  24.     @Override
  25.     public void onStop()
  26.     {
  27.         super.onStop();
  28.         _initTask.cancel(true);
  29.     }
  30.  
  31.     protected class InitTask extends AsyncTask<Context, String, SearchResponse>
  32.     {
  33.         @Override
  34.         protected SearchResponse doInBackground( Context... params )
  35.         {
  36.             InputStream source = retrieveStream(url);
  37.             SearchResponse response = null;
  38.             if (source != null) {
  39.                 Gson gson = new Gson();
  40.                 Reader reader = new InputStreamReader(source);
  41.                 try
  42.                 {
  43.                     response = gson.fromJson(reader, SearchResponse.class);
  44.                     publishProgress( response.query );
  45.                 } catch (JsonSyntaxException e)
  46.                 {
  47.                     Log.w(getClass().getSimpleName(), "Error: " + e.getMessage() + " for URL " + url);
  48.                     return null;
  49.                 } catch (JsonIOException e)
  50.                 {
  51.                     Log.w(getClass().getSimpleName(), "Error: " + e.getMessage() + " for URL " + url);
  52.                     return null;
  53.                 } finally
  54.                 {
  55.                     try
  56.                     {
  57.                         reader.close();
  58.                     } catch (IOException e)
  59.                     {
  60.                         Log.w(getClass().getSimpleName(), "Error: " + e.getMessage() + " for URL " + url);
  61.                     }
  62.                 }
  63.             }
  64.             if (!this.isCancelled()) {
  65.                 return response;
  66.             } else {
  67.                 return null;
  68.             }
  69.         }
  70.  
  71.         @Override
  72.         protected void onProgressUpdate(String... s)
  73.         {
  74.             super.onProgressUpdate(s);
  75.             Toast.makeText(getApplicationContext(), s[0], Toast.LENGTH_SHORT).show();
  76.         }
  77.  
  78.         @Override
  79.         protected void onPostExecute( SearchResponse response )
  80.         {
  81.             super.onPostExecute(response);
  82.             StringBuilder builder = new StringBuilder();
  83.             if (response != null)
  84.             {
  85.                 String delim = "* ";
  86.                 List<Result> results = response.results;
  87.                 for (Result result : results) {
  88.                     builder.append(delim).append(result.hotelName_);
  89.                     delim="\n* ";
  90.                 }
  91.             }
  92.             if (builder.length() > 0) {
  93.                 Toast.makeText(getApplicationContext(), builder.toString(), Toast.LENGTH_SHORT).show();
  94.             } else {
  95.                 Toast.makeText(getApplicationContext(), "The response was empty.", Toast.LENGTH_SHORT).show();
  96.             }
  97.  
  98.         }
  99.  
  100.         @Override
  101.         protected void onCancelled()
  102.         {
  103.             super.onCancelled();
  104.             Toast.makeText(getApplicationContext(), "The operation was cancelled.", 1).show();
  105.         }
  106.  
  107.         private InputStream retrieveStream(String url)
  108.         {
  109.             DefaultHttpClient client = new DefaultHttpClient();
  110.             HttpGet getRequest;
  111.             try {
  112.                 getRequest = new HttpGet(url);
  113.                 try {
  114.                     HttpResponse getResponse = client.execute(getRequest);
  115.                     final int statusCode = getResponse.getStatusLine().getStatusCode();
  116.                     if (statusCode != HttpStatus.SC_OK) {
  117.                         Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url);
  118.                         return null;
  119.                     }
  120.                     HttpEntity getResponseEntity = getResponse.getEntity();
  121.                     try {
  122.                         return getResponseEntity.getContent();
  123.                     } catch (IllegalStateException e) {
  124.                         getRequest.abort();
  125.                         Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
  126.                         return null;
  127.                     } catch (IOException e) {
  128.                         getRequest.abort();
  129.                         Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
  130.                         return null;
  131.                     }
  132.                 } catch (ClientProtocolException e) {
  133.                     getRequest.abort();
  134.                     Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
  135.                 } catch (IOException e) {
  136.                     getRequest.abort();
  137.                     Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
  138.                 }
  139.             } catch (IllegalArgumentException e) {
  140.                 Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
  141.             }
  142.             return null;
  143.         }
  144.  
  145.     }
  146.  
  147. }
Parsed in 0.053 seconds, using GeSHi 1.0.8.4


Now I have two other classes which I am not to sure why I am using this code is from a Tutorial.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Result
  2. {
  3.    
  4.     @SerializedName("hotelAddress")
  5.     public String hotelAddress_;
  6.    
  7.     @SerializedName("hotelDescription")
  8.     public String hotelDescription_;
  9.    
  10.     @SerializedName("hotelName")
  11.     public String hotelName_;
  12.    
  13.     @SerializedName("idhotelTable")
  14.     public int idhotelTable_;
  15.  
  16.  
  17.    
  18.  
  19.    
  20. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class SearchResponse
  2. {
  3.    
  4.     public List<Result> results;
  5.    
  6.    
  7.     public String query;
  8.    
  9. }
  10.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



So basically I have the Internet permissions added. The URL seems to be right. Just when I click the button the emulator force closes the application. Can anyone please help me!

The json which it reads in from the URL : http://localhost:8080/hotelDB/resources ... hoteltable displays on the page this JSON object.

{"hotelTable":[{"hotelAddress":"Temple Bar","hotelDescription":"Right in the heart of Dublin","hotelName":"Temple Bar Hotel","idhotelTable":"1"},{"hotelAddress":"Liffey Valley","hotelDescription":"lovely hotel with great night life","hotelName":"Clarion Hotel","idhotelTable":"2"},{"hotelAddress":"Fleet Street","hotelDescription":"Little bit creepy","hotelName":"Fleet Street Hotel","idhotelTable":"3"}]}

I want to connect to the page read it in, find the hotel name part of the object and make it equal to a makeText toast. PLEASE HELP ME!!
User avatar
PhilKearney
Freshman
Freshman
 
Posts: 4
Joined: Thu Apr 14, 2011 6:28 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 8 guests