Reputation: 13
I am learning android. I started working on networking with android. The problem i m facing is that under the try statement the httpclient.execute(request) line is not working. Instead the catch clause is executed every time. I have searched for quite a long time but i m not getting answers regarding the problem. i'll be pleased to get any kind of help from ur side.
Here is the main activity
package com.example.androidaspect;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
String url = "http://www.google.com";
TextView tv = (TextView) findViewById(R.id.tvres);
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute(url, context, tv);
}
}
Here is the Asynctaskrunner
package com.example.androidaspect;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;
public class AsyncTaskRunner extends AsyncTask<String, String, String>{
Context context;
TextView tv;
String url;
String result="";
public void execute(String url, Context context, TextView tv) {
// TODO Auto-generated method stub
this.tv= tv;
this.context = context;
this.url = url;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
publishProgress("Loading");
HttpClient hc = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse res = hc.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
String line="";
while((line=rd.readLine())!=null){
result.concat(line);
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tv.setText(result);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
Toast.makeText(context, values[0], Toast.LENGTH_SHORT).show();
}
}
I have added the internet permission. i have a textview in my xml which is getting the response from the website. but when i run the app on the emulator, it runs but the textview does not load the data.
Upvotes: 0
Views: 474
Reputation: 133570
As i mentioned in the comment use AsyncTask.
Quoting docs
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged
http://developer.android.com/reference/android/os/AsyncTask.html
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tvres);
tv = (TextView) findViewById(R.id.textView1);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... arg0) {
// TODO Auto-generated method stub
String response=null;
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet("http://www.google.com");
HttpResponse res = hc.execute(hg);
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
tv.setText(result);
}
}
You have AsyncTask as a inner class.
class TheTask extends AsyncTask<Void,Void,String>
You do not pass anything to doInbackground
.So the first arg type is Void. The second arg is also Void coz you do not have a progress indicator. The third is the type of the result of the background computation. So its string since we have return response;
which is string.
Now doInbackground
is invoked on the background thread. SO do your background computation there.
onPostExecute
is invoked on the ui thread. So update ui there.
Edit:
Your way
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.google.com";
tv = (TextView) findViewById(R.id.tvres);
AsyncTaskRunner runner = new AsyncTaskRunner(url,this,tv);
runner.execute();
}
}
Then
public class AsyncTaskRunner extends AsyncTask<Void, String, String>{
Context context;
TextView tv;
String url;
String result="";
public AsyncTaskRunner(String url, Context context, TextView tv) {
// TODO Auto-generated method stub
this.tv= tv;
this.context = context;
this.url = url;
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
publishProgress("Loading");
String response=null;
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet(url);
HttpResponse res = hc.execute(hg);
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity);
}catch(Exception e){
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tv.setText(result);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
Toast.makeText(context, values[0], Toast.LENGTH_SHORT).show();
}
}
Upvotes: 2
Reputation: 7872
You need to use AsyncTask<> or IntentService to handle HttpQueries
Upvotes: 0