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