In addition to what Romain Guy wrote, here are some other problems with
your code:
Persona wrote:
> hi, the code below was designed to display text within android (this
> is a response from the web server). But somehow the code doesn't work-
> looking at it I don't see any problem, and there are no errors
> reported. The application runs and no code is displayed.
>
> Can somebody with greater experience uncover any problem within the
> program?
<snip>
> public class TestHttpClient extends Activity
> {
> @Override
> public void onCreate(Bundle icicle){
> super.onCreate(icicle);
> setContentView(R.layout.main);
> new HttpConnect1().execute();
> }
>
> private class HttpConnect1 extends AsyncTask<String, Void, Void>
> {
> String result;
> @Override
> protected Void doInBackground(String...strings)
You are supposed to be passing in strings to execute(), and you're not.
Now, I am definitely fuzzy on my Java varargs, so it may be your code is
still getting executed here. But I would either switch this to Void or
pass "http://www.cnn.com" in your execute() method above.
> {
> BufferedReader in = null;
> try {
> HttpClient client = new DefaultHttpClient();
> HttpPost request = new
> HttpPost("http://www.cnn.com");
> List<NameValuePair> postParameters = new
> ArrayList<NameValuePair>
> ();
> postParameters.add(new BasicNameValuePair("one",
> "valueGoesHere"));
> UrlEncodedFormEntity formEntity = null;
> try {
> formEntity = new
> UrlEncodedFormEntity(postParameters);
> } catch (UnsupportedEncodingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> request.setEntity(formEntity);
> HttpResponse response = client.execute(request);
You might consider switching to BasicResponseHandler to simplify your code:
http://bit.ly/lCVgZ
> in = new BufferedReader(new
> InputStreamReader(response.getEntity
> ().getContent()));
> StringBuffer sb = new StringBuffer("");
> String line = "";
> String NL =
> System.getProperty("line.separator");
> while ((line = in.readLine()) != null) {
> sb.append(line + NL);
> }
> in.close();
> result = sb.toString();
>
> } catch (Exception e) {
> e.printStackTrace();
> } finally
> {
> if (in != null) {
> try {
> in.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
> return null;
> }
> //@Override
> protected void onPostExecute(String result)
> {
> TextView tv = new TextView(TestHttpClient.this);
> tv.setText(result);
> }
> }
But the big problem (besides your created-but-unused TextView) is that
you declared your class as <String, Void, Void>, meaning that
onPostExecute() should take no parameters. However, your implementation
takes a String parameter. If you uncomment your @Override annotation,
you should get a compiler error pointing this out.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
Android Development Wiki: http://wiki.andmob.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---