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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测