0

I am trying to import text from a web page hosted on the server I have the java file as below

public class News extends Activity
{
    TextView txtview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.txt_news);
        txtview =(TextView) findViewById(R.id.textView1); 

        try
        {
            HttpClient htc=new DefaultHttpClient();
            HttpPost htp=new HttpPost("http://www.aaaa.com/a.php");

            HttpResponse res=htc.execute(htp);
            HttpEntity ent=res.getEntity();
                    InputStream web=ent.getContent();
            try
            {
                BufferedReader reader=new BufferedReader(new InputStreamReader(web,"iso-8895-1"),8);
                txtview.setText(reader.readLine());
                web.close();

            }
            catch(Exception e)
            {
                Log.e("log_tag","Error"+e.toString());

            }



        } catch(Exception e)
        {
            Log.e("log_tag","Error"+e.toString());

        }
            finally
            {

            }


}
}

and the layout xml coded like :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/news_bg"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="240dp"

        android:textColor="#000" />

    </RelativeLayout>

In manifest i have also included the internet connection permission syntax but i dont get any text from hosted file a.php

the code in a.php:

<?php

$t="hello buddy";
print json_encode($t);

?>

please help to resolve the problem the text view is not showing the $t variable content hello buddy.how to do that.... screen of layout.xml appears but there is no text in textview.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
user2845758
  • 21
  • 1
  • 8
  • 2
    If you checked your logcat I guess you will see error of the catch.. basically, you can't do any networking in Android without Threads. and you should use [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) – Coderji Apr 19 '14 at 05:07

2 Answers2

1

As @Coderji suggested you are calling network operation in MainThread. You should not call Costly operations like Getting data from server on Main thread. If you want to clearly understand this visit this android.os.NetworkOnMainThreadException

And here If you want to get hello buddy string then you should not call print json_encode($t);. As print operation will print the hello world string in web page format there may be chance that you get html data instead what you need.

Follow this tutorial if you want to use write web services in your application. Android Login and Registration with PHP, MySQL and SQLite

Community
  • 1
  • 1
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

In the above code you are trying to make a network call in main UI thread. We should not ideally do any long running operations like network call, reading disks,etc., in main UI thread.

You can use the AysncTask (http://developer.android.com/reference/android/os/AsyncTask.html) something as follows to achieve the desired functionality.

private class FetchContentTask extends AsyncTask<void, void, String> {
         protected String doInBackground(URL... urls) {
                HttpClient htc=new DefaultHttpClient();
                HttpPost htp=new HttpPost("http://www.aaaa.com/a.php");

                HttpResponse res=htc.execute(htp);
                HttpEntity ent=res.getEntity();
                        InputStream web=ent.getContent();
                try
                {
                    BufferedReader reader=new BufferedReader(new InputStreamReader(web,"iso-8895-1"),8);
                    return reader.readLine()
                    web.close();


                }
                catch(Exception e)
                {
                    Log.e("log_tag","Error"+e.toString());

                }
         }

         protected void onPostExecute(String result) {
             txtview.setText();
         }
     }
keerthy
  • 236
  • 1
  • 9