-1

I have a hosted website in php. The android app tries to open a web page happy.php. When I try to run this app, instead of displaying yayyyy in the textview, failed is shown. Please help me to find the error

public class MainActivity extends ActionBarActivity {
    Button b;
    TextView t;
    String n;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button);
        t=(TextView)findViewById(R.id.textView);
        b.setOnClickListener( new View.OnClickListener(){
            public void onClick(View v)
            {try {
                DefaultHttpClient d = new DefaultHttpClient();
                HttpPost p = new HttpPost("http://www.palakarora.net16.net/happy.php");
                HttpResponse httpResponse = d.execute(p);
                HttpEntity httpEntity = httpResponse.getEntity();
                InputStream is = httpEntity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                n = reader.readLine();
            }
            catch(Exception e)
            {
                n="failed";
            }
                t.setText(n);



            }
        });
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

happy.php is the web page called by android app. the page prints "yayyyy". happy.php

<?
print("yayyyyy");
?>
alice
  • 51
  • 7

3 Answers3

2

All android async tasks (including connections) should be made in a separate threads, and in all modern versions, you are forced to make connection in a separate thread (using AsyncTask or something like that), the bad thing is that the connection problem is not shown specifically when this happens, so make sure you are doing this when sending your request. Hope this helps

angrykoala
  • 3,774
  • 6
  • 30
  • 55
0

Indeed, networking on main thread is not allowed.

Perhaps use Retrofit library to deal with the downloading.

Life is better without asynctasks

http://square.github.io/retrofit/

xorgate
  • 2,244
  • 1
  • 24
  • 36
0

It's indifferent that you work in local or with server, the 'httpPost' class need work inside of AsynClass, because the 'httpPost' is async method... I explain a little bit here and has 3 examples!!

Tell me if I helped you and good programming!

Community
  • 1
  • 1
  • I wasted my entire day on this but couldn't rectify it. This was very helpful. Thank you sooooooo much :) – alice Jun 30 '15 at 17:20