0

I'm trying to develop a simple app that gets data from web service and displays it

It throws exception exactly when he calls response.execute(client)

package com.example.webbasedapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;

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.util.Log;

public class GETMethods {

public String getInternetData() throws Exception{
    BufferedReader in=null;
    String Data=null;
    try{

        HttpClient client=new DefaultHttpClient();
        URI web=new URI("http://www.mybringback.com/");
        HttpGet request = new HttpGet();
        request.setURI(web);
        HttpResponse reponse=client.execute(request);
        Log.v("response code", reponse.getStatusLine()
                .getStatusCode() + ""); 
        InputStream inp=reponse.getEntity().getContent();
        in=new BufferedReader(new InputStreamReader(inp));
        StringBuffer buf=new StringBuffer();
        String l="";
        String nl=System.getProperty("line.separator");
        while((l=in.readLine())!=null){
            buf.append(l+nl);
        }
        in.close();
        Data=buf.toString();
        return Data;

    }finally{
        if(in!=null){
            try{
                in.close();
                return Data;
            }catch (Exception e){
                Log.d("error",e.toString());
            }
        }
    }
}
}

And this is my main activity

package com.example.webbasedapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    TextView test = (TextView) findViewById(R.id.data);
    GETMethods data = new GETMethods();
    String d = null;
    try {
        d = data.getInternetData();
        // test.setText(d);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        d = "bla";
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        // Log.d("testW",e.getMessage());
    }
    test.setText(d);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

}
rene
  • 41,474
  • 78
  • 114
  • 152
a3adel
  • 1,223
  • 4
  • 15
  • 26

1 Answers1

4

You are attempting to access the network on the UI thread which is not allowed due to potential hangup issues. Look into AysncTask and implement GETMethods as one of them. It will look something like this:

public class GETMethods extends AsyncTask<String, Void, String>{
    protected void doInBackground(String... params){
        YourNetworkCode
    }
}
Brad Rem
  • 6,036
  • 2
  • 25
  • 50
user2483079
  • 533
  • 2
  • 10