douba8758 2014-04-19 05:04
浏览 27
已采纳

如何从Android应用程序中的网站导入文本

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.

  • 写回答

2条回答 默认 最新

  • dtpxi88884 2014-04-19 05:15
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端