The following code should access a website read the XML and display it on the Layout. Somehow isn't working on my Android while on the Emulator it is working fine. Can someone enlighten me why it's not working and what should I do to correct it?
Java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void connect (View v){
TextView mTextView = (TextView) findViewById(R.id.textView1);
try {
Socket mySocket = new Socket("www.ynet.co.il", 80);
InputStream in = mySocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = "";
String output = "";
while ((line = reader.readLine()) != null) {
output += line;
}
mTextView.setText(output);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void good (View v){
TextView mTextView = (TextView) findViewById(R.id.textView1);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.boi.org.il/currency.xml?curr=01");
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStreamReader in = new InputStreamReader(entity.getContent());
BufferedReader reader = new BufferedReader(in);
String line = "";
String output = "";
while ((line = reader.readLine()) != null) {
output += line;
if (line.contains("RATE")) {
StringTokenizer tokens = new StringTokenizer(line,"><");
String token = tokens.nextToken();
String token1 = tokens.nextToken();
String token2 = tokens.nextToken();
mTextView.setText("The USD/ILS Rate is: "+token2);
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
This is the LogCat:
02-17 17:55:39.165: D/dalvikvm(14064): Late-enabling CheckJNI
02-17 17:55:39.225: I/dalvikvm(14064): Turning on JNI app bug workarounds for target SDK version 10...
02-17 17:55:39.280: W/ActivityThread(14064): Application com.example.httpsocket can be debugged on port 8100...
02-17 17:55:40.905: D/AndroidRuntime(14064): Shutting down VM
02-17 17:55:40.905: W/dalvikvm(14064): threadid=1: thread exiting with uncaught exception (group=0x410ed2a0)
02-17 17:55:40.935: E/AndroidRuntime(14064): FATAL EXCEPTION: main
02-17 17:55:40.935: E/AndroidRuntime(14064): java.lang.IllegalStateException: Could not execute method of the activity
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View$1.onClick(View.java:3691)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View.performClick(View.java:4211)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View$PerformClick.run(View.java:17267)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.Handler.handleCallback(Handler.java:615)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.Handler.dispatchMessage(Handler.java:92)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.Looper.loop(Looper.java:137)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 17:55:40.935: E/AndroidRuntime(14064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-17 17:55:40.935: E/AndroidRuntime(14064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-17 17:55:40.935: E/AndroidRuntime(14064): at dalvik.system.NativeStart.main(Native Method)
02-17 17:55:40.935: E/AndroidRuntime(14064): Caused by: java.lang.reflect.InvocationTargetException
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View$1.onClick(View.java:3686)
02-17 17:55:40.935: E/AndroidRuntime(14064): ... 11 more
02-17 17:55:40.935: E/AndroidRuntime(14064): Caused by: android.os.NetworkOnMainThreadException
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-17 17:55:40.935: E/AndroidRuntime(14064): at com.example.httpsocket.MainActivity.good(MainActivity.java:63)
02-17 17:55:40.935: E/AndroidRuntime(14064): ... 14 more