* Hello, I have tried to impliment the suggested solution but I am having a
problem with the doInBackground(). I need this to return the String so that
I can process this in the onPostExecute() to update the TextView widget. But
there is an error in the 'public String doInBackground(String...params)'.
The String (return type) is said to be incompatible with the
AsyncTask<String, Void, Void)... Apparently the two options are to implement
or override doInBackground(). I think that is what I have been doing. What
am I do wrong?


package* http.client;

*

import* java.io.BufferedReader;
*

import* java.io.IOException;
*

import* java.io.InputStreamReader;
*

import* java.io.UnsupportedEncodingException;
*

import* java.net.URL;
*

import* java.util.ArrayList;
*

import* java.util.List;

*

import* org.apache.http.HttpResponse;
*

import* org.apache.http.NameValuePair;
*

import* org.apache.http.client.HttpClient;
*

import* org.apache.http.client.entity.UrlEncodedFormEntity;
*

import* org.apache.http.client.methods.HttpPost;
*

import* org.apache.http.impl.client.DefaultHttpClient;
*

import* org.apache.http.message.BasicNameValuePair;

*

import* android.app.Activity;
*

import* android.os.AsyncTask;
*

import* android.os.Bundle;
*

import* android.view.View;
*

import* android.widget.TextView;

*public* *class* TestHttpClient *extends* Activity

{

@Override

*public* *void* onCreate(Bundle icicle){

*super*.onCreate(icicle);

setContentView(R.layout.*main*);

TextView myTextView = *new* TextView(*this*);

String result = *new* HttpConnect1().doInBackground();

*new* HttpConnect1().onPostExecute(myTextView, result);

}

*private* *class* HttpConnect1 *extends* AsyncTask<String, Void, Void>

{

@Override

*public* *String* doInBackground(String...params)

{

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 = *new*UrlEncodedFormEntity(postParameters);

request.setEntity(formEntity);

HttpResponse response = client.execute(request);

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();

String result = sb.toString();

} *finally* {

*if* (in != *null*) {

*try* {

in.close();

} *catch* (IOException e) {

e.printStackTrace();

}

}

}

}

*public* *void* onPostExecute(TextView myTextview, String result)

{

String result1 = result;

myTextview.setText(result1);

setContentView(myTextview);

}

}

}



On Wed, Jul 8, 2009 at 1:50 AM, Mark Murphy <[email protected]> wrote:

>
> Persona wrote:
> > Hello, the following code uses POST method to communicate with the Web
> > Server, and the response is displayed in the console (not parsed in a
> > Servlet, etc). When compiled as a java application, the program works
> > as expected returning the response as html text.
> >
> > My aim is to have an Android app that will communicate with servers
> > using various protocols- and I am using this as an example in learning
> > how that technique can be done. Therefore I would wish Android to get
> > the response, and displaying it in 'TextView?' or somewhere. I thought
> > this was going to be a straight-forward thing, but I have been stuck
> > at this point for days now.
> >
> > Can somebody assist if possible.
>
> You should try some of the tutorials on http://developer.android.com.
> What you have is fine for desktop Java but bears little resemblance to
> an Android app, any more than it bears much resemblance to a Swing or
> SWT app.
>
> Step #1: Get the Hello, World tutorial working
>
> http://developer.android.com/guide/tutorials/hello-world.html
>
> Step #2: Read up on AsyncTask
>
> Step #3: Create an AsyncTask that, in its doInBackground() method, does
> your HTTP processing, and in its onPostExecute() method, updates the
> TextView widget
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com | http://twitter.com/commonsguy
>
> _Android Programming Tutorials_ Version 1.0 In Print!
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to