Possible Duplicate:
Calling Method in Android
I’m trying to contatct an API. I have yet to see if its even working…but my test message is not even working.
Here’s my MainActivity:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.test.MESSAGE";
@SuppressLint("ParserError")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendIP(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
Here’s the DisplayMessageActivity:
-“hello” should be written on the screen, but instead its blank. I figure if that’s not showing up, then my request probably didn’t make it anywhere either, so advice on that would be helpful as well.
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.site/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String test = "hello";
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(test);
//TextView myTextView = (TextView) findViewById(R.id.myTextView);
// myTextView.setText(test);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
And I have allowed access to internet in the manifest!
Thanks!
You need a setContentView() to an xml layout or directly to your textview in the onCreate() of your DisplayMessageActivity.
You also need to move the network operation to an AsyncTask or otherwise get it off the UI thread. While some of the old official samples are bad about this, it’s never been a good idea, and on more recent Android versions will result in a proactive crash.