[android-developers] Re: HttpResponse problem

2011-09-20 Thread drpickett
I usually hate it when people just reply with a snarky dope-slap
response, but in this case it is merited somewhat.  The compiler error
would be helpful in diagnosing the problem.

dp

On Sep 19, 1:58 am, swer ademk...@gmail.com wrote:
 hello friends..

 It has been almost a day and I have still working on this little code.
 All I need to do is to get the response stream from the server.

 However compiler gives an error just the beginning of the code..
 I will be appreciated if you give me a hand  on this...

 The code is below.
 Note :  I also set the INTERNET permission in the manifest file.
 Thanks in advance..

 swer

 //
 *** 
 ***

 public class AsyncTestActivity extends Activity implements
 OnClickListener {
             /** Called when the activity is first created. */
                 Button btnThread;
                 EditText txtEdit;
                 public String threadName = ;

             @Override
             public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.main);

                 btnThread  = (Button)findViewById(R.id.btnThread);
                 btnThread.setOnClickListener(this);

                 txtEdit = (EditText)findViewById(R.id.txtEdit);
             }

                 @Override
                 public void onClick(View v) {
                         // TODO Auto-generated method stub
                         grabURL(http://www.google.com/;);
                 }

             public void grabURL(String url) {
                 new GrabURL().execute(url);
             }

  private class GrabURL extends AsyncTaskString, Void, Void {
                 private HttpClient Client =null;
                 private String strContent;
                 private String strError = null;
                 private ProgressDialog Dialog = new
 ProgressDialog(AsyncTestActivity.this);

                 protected void onPreExecute() {
                     Dialog.setMessage(Downloading source..);
                     Dialog.show();
                 }

                 protected Void doInBackground(String... urls) {
                     try {
                           // Create client and set our specific user-agent
 string
                           HttpGet request = new HttpGet(urls[0]);
                           request.setHeader(User-Agent, Android);
                           Client = new DefaultHttpClient(httpParameters);

                           HttpResponse response =
 Client.execute(request);                    //  COMPILER GIVES
 AN ERROR AT THIS POINT  ang goes to onPostExecute(void);

                         //   ResponseHandlerString responseHandler  = new
 BasicResponseHandler();
                         //   strContent = Client.execute(request,
 responseHandler);

                             // Check if server response is valid
                           StatusLine status = response.getStatusLine();
                           if (status.getStatusCode() != 200) {
                               throw new IOException(Invalid response from
 server:  + status.toString());
                           }

                           // Pull content stream from response
                           HttpEntity entity = response.getEntity();
                           InputStream inputStream = entity.getContent();

                           ByteArrayOutputStream content = new
 ByteArrayOutputStream();

                           // Read response into a buffered stream
                           int readBytes = 0;
                           byte[] sBuffer = new byte[512];
                           while ((readBytes = inputStream.read(sBuffer)) !=
 -1) {
                               content.write(sBuffer, 0, readBytes);
                           }

                           // Return result from buffered stream
                           String dataAsString = new
 String(content.toByteArray());

                           txtEdit.setText(dataAsString);

                       } catch (IOException e) {
                                 strError = e.getLocalizedMessage();
                                 cancel(true);

                     }

                     return null;
                 }

                 protected void onPostExecute(Void unused) {
                     Dialog.dismiss();
                     if (strError != null) {
                         Toast.makeText(AsyncTestActivity.this, strError,
 Toast.LENGTH_LONG).show();
                     } else {
                         Toast.makeText(AsyncTestActivity.this, Source:  +
 strContent, Toast.LENGTH_LONG).show();
                     }
                 }

             }

         }
 //
 *** 
 ***

-- 
You received this 

Re: [android-developers] Re: HttpResponse problem

2011-09-20 Thread Daniel Drozdzewski
I  don't mind snotty replies to crap questions.

There are far too many posts that are not questions, or questions that
were answered on this list, or Stack Overflow, or simple google-fu for
the key terms in question gives the answer within first 3 results.

About the code below:

[...]

Client = new DefaultHttpClient(httpParameters);


... I cannot see, where httpParameters are defined?


Have a look at following example. Analyse it with reading API and see,
where the problem lies:
http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/


-- 
Daniel Drozdzewski

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en