Reputation: 1
This is android code to fetch data from internet bu when I run this code no data is displayed
public class data
{
public String a()throws Exception
{
String data=null;
BufferedReader b=null;
try
{
HttpClient ob=new DefaultHttpClient();
URI website=new URI("http://www.mybringback.com");
HttpGet request=new HttpGet();
request.setURI(website);
HttpResponse response= ob.execute(request);
b=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer bu=new StringBuffer ();
String i = null;
while((i=b.readLine())!=null)
{
bu.append(i);
}
b.close();
data=bu.toString();
return data;
}finally
{
if (b!=null)
{
try {
b.close();
return data;
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
My main class
package com.example.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.HttpEntity;
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 java.io.*;
import java.net.URI;
public class MyActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView m =(TextView)findViewById(R.id.de);
data ob=new data();
try {
String v= ob.a();
m.setText(v);
} catch (Exception e) {
e.printStackTrace();
}
}
}
My xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
android:id="@+id/de">
</TextView>
</ScrollView>
</LinearLayout>
Upvotes: 0
Views: 81
Reputation: 133570
You are running network related operation on the ui thread. Probably getting NetworkOnMainThreadException
. Use Thread
or Asynctask
.
http://developer.android.com/reference/android/os/AsyncTask.html
Use Asynctask
if the network operation is for a short duration.
You can make Asynctask
an inner class an update ui in onPostExecute
.
Also follow java naming conventions and re-name your variables and methods which helps readability
Example:
public class MyActivity extends Activity
{
TextView m;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m=(TextView)findViewById(R.id.de);
new TheTask().execute()
}
class TheTask extends AsyncTask<Void,Void,String>
{
String _response=null;
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try
{
HttpClient ob=new DefaultHttpClient();
URI website=new URI("http://www.mybringback.com");
HttpGet request=new HttpGet();
request.setURI(website);
HttpResponse response= ob.execute(request);
HttpEntity resEntity = response.getEntity();
_response=EntityUtils.toString(resEntity);
}catch(Exception e)
{
e.printStackTrace();
}
return _response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
m.setText(result);
}
}
}
Upvotes: 1