title: Android currency converter application
body: <p>I'm working on a big program for Android and I'm trying to get the currency converter part of the program to work. For full disclosure: I found it from <a href="http://firstamong.com/building-android-currency-converter/" rel="nofollow">http://firstamong.com/building-android-currency-converter/</a>, and it's a tutorial on how to build a real-time currency converter. I'll post the code in question followed by the logcat. The error that occurs is when I try to convert from one currency to another, the application says it was forced to stop. However, the "Invalid" portion works (the case where both currency fields are the same.) Any help would truly be appreciated:</p>
  
  <p>Code:</p>
  
  <pre><code>public class currency_converter extends Activity {
  
  public int to;
  public int from;
  public String [] val;
  public String s;
  public Handler handler;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_currency_converter);
      Spinner s1 = (Spinner) findViewById(R.id.spinner1);
      Spinner s2 = (Spinner) findViewById(R.id.spinner2);
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(
              this, R.array.name, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
      val  = getResources().getStringArray(R.array.value);
      s1.setAdapter(adapter);
      s2.setAdapter(adapter);
      s1.setOnItemSelectedListener(new spinOne(1));
      s2.setOnItemSelectedListener(new spinOne(2));
      Button b = (Button) findViewById(R.id.button1);
      b.setOnClickListener(new OnClickListener(){
          public void onClick(View v) {
              TextView t = (TextView) findViewById(R.id.textView4);
              if(from == to)
              {
                  Toast.makeText(getApplicationContext(), "Invalid", 4000).show();
              }
              else
              {
                  try {
                      s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&amp;format=json&amp;diagnostics=true&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&amp;callback=");
                      JSONObject jObj;
                      jObj = new JSONObject(s);
                      String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
                      t.setText(exResult);
                  } catch (JSONException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  catch (ClientProtocolException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
      });
  }
  public String getJson(String url)throws ClientProtocolException, IOException {
  
      StringBuilder build = new StringBuilder();
      HttpClient client = new DefaultHttpClient();
      HttpGet httpGet = new HttpGet(url);
      HttpResponse response = client.execute(httpGet);
      HttpEntity entity = response.getEntity();
      InputStream content = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(content));
      String con;
      while ((con = reader.readLine()) != null) {
          build.append(con);
      }
      return build.toString();
  }
  private class spinOne implements OnItemSelectedListener
  {
      int ide;
      spinOne(int i)
      {
          ide =i;
      }
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                                 int index, long id) {
          if(ide == 1)
              from = index;
          else if(ide == 2)
              to = index;
  
      }
  
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          // TODO Auto-generated method stub
      }
  class GetResponseData extends AsyncTask&lt;String, String, String&gt; {
          private ProgressDialog dialog;
          private ArrayList&lt;String&gt; titleList;
          private TextView textView;
  
          public GetResponseData(TextView textView) {
              this.textView = textView;
          }
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              dialog = ProgressDialog.show(currency_converter.this, "", "Loading",
                      false);
          }
  
          @Override
          protected String doInBackground(String... params) {
  
              try {
                  String s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22" + val[from] + val[to] + "%22)&amp;format=json&amp;diagnostics=true&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&amp;callback=");
                  JSONObject jObj;
                  jObj = new JSONObject(s);
                  String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
                  return exResult;
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return null;
              } catch (ClientProtocolException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return null;
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  return null;
              }
          }
  
          @Override
          protected void onPostExecute(String result) {
              super.onPostExecute(result);
              if (dialog != null)
                  dialog.dismiss();
              if (result != null) {
                  textView.setText(result);
              }
          }
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Logcat:</p>
  
  <pre><code>10-30 00:59:48.164  20591-20591/com.example.travelapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
  android.os.NetworkOnMainThreadException
          at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
          at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
          at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
          at java.net.InetAddress.getAllByName(InetAddress.java:214)
          at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
          at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
          at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
          at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
          at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:587)
          at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
          at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
          at com.example.travelapplication.currency_converter.getJson(currency_converter.java:93)
          at com.example.travelapplication.currency_converter$1.onClick(currency_converter.java:68)
          at android.view.View.performClick(View.java:4222)
          at android.view.View$PerformClick.run(View.java:17620)
          at android.os.Handler.handleCallback(Handler.java:800)
          at android.os.Handler.dispatchMessage(Handler.java:100)
          at android.os.Looper.loop(Looper.java:194)
          at android.app.ActivityThread.main(ActivityThread.java:5391)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:525)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>