I am trying to call a method I have written. It compiles except for one line…
public class http extends Activity {
httpMethod(); //will not compile
public void httpMethod(){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://site/api/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String test = "hello";
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
}
}
}
I’m not the best java guy, but I would think calling the method like so would get a response. “Hello” is not displayed however…
How do I properly call the method?
EDIT: Just to leave no-one in any doubt, this answer only addresses why you’re getting a compile-time error. It does not address what you should be doing in which thread and at what time in Android.
Personally I would recommend that you put Android down for the moment, learn Java in a simpler environment (e.g. console apps) and then, when you’re comfortable with the language, revisit Android and learn all the requirements of Android development – which are obviously much more than just the language.
You’re trying to call a method as a statement directly within your class. You can’t do that – it has to be part of a constructor, initializer block, other method, or static initializer. For example:
Note that I’ve only given this example to show you a valid Java class. It doesn’t mean you should actually be calling the method from your constructor.