Background:
I have started Android recently. I want to make an application for Android that fetches data from a server and customizes it , displays it and then use Twitter to tweet the results. I am thinking to use (twitter4j API for this.).
Initially i have a PERL file on server that i need to call from my application’s interface. (I have modified code of HelloWorld.java available at (dev.android..). The PERL file which i have stored on the server has the output in form of print “” I would be using the collective print output and decode them in my application.
Now my code is as follows :
package com.example.helloandroid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldAndroidActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
/* tv.setText("Hello, World !! This is my First Android App .. Cheers");
setContentView(tv);*/
try {
InputStream is = new URL("http://myserver.com:1941/cgi-bin/myperl.pl").openStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String str = in.readLine();
in.close();
tv.setText(str);
setContentView("phew");
} catch (MalformedURLException e) {
//FAIL
} catch (IOException e) {
//FAIL
}
}
}
However i am not able to use the code to get the data from the server file as Eclipse emulator shows up but does not displays anything except the “shinny android logo”.
Is there any way i can read that file? Also i would like you seniors to suggest me some startup/dummies book fro Android development.
How long do you want for the emulator to boot up? It can take upwards of two minutes to boot up. It’s kind of slow in that respect. Try giving it a little time to actually get to the home screen. Also, is your logcat saying anything interesting?
Also, you’re doing a network operation on the main UI thread. NEVER do this. You need to move your network operations on to a different thread. For more details, read Painless Threading.