不读书的服务器文本 [英] Not Reading text from server

查看:85
本文介绍了不读书的服务器文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取服务器上所有的文字,我觉得我的code未检索来自服务器的文本将请你帮我,我怎么能解决这个问题,我是新来的android开发。在此先感谢

 公共类TestActivity延伸活动{

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    TextView的电视=(TextView中)findViewById(R.id.my_text);


                尝试 {

                    网址URL =新的URL(http://www.google.com:80/);
                    StringBuilder的内容=新的StringBuilder();
                    由服务器返回//读取文本

                    的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
                    串线;
                    而((行= in.readLine())!= NULL){
                        content.append(行+\ N);
                    }
                    tv.setText(content.toString());
                    的setContentView(电视);

                    附寄();
                }

                赶上(MalformedURLException的E){

                    的System.out.println(格式不正确的URL:+ e.getMessage());

                }

                赶上(IOException异常E){

                    的System.out.println(I / O错误:+ e.getMessage());

                }


};


}
 

解决方案

可能是你可以尝试这样的,只是一个想法只有:

  StringBuilder的内容=新的StringBuilder();
                由服务器返回//读取文本
                的BufferedReader在=新的BufferedReader(新InputStreamReader的(新的URL(http://www.google.com:80/).openConnection()的getInputStream()));
                串线;
                而((行= in.readLine())!= NULL){
                    content.append(行+\ N);
                }

tv.setText(content.toString());
的setContentView(电视);
 

I am trying to read all the text from server, I think that my code is not retrieving text from server will you please help me how can I fix this problem, I am new to android development. Thanks in advance

public class TestActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv= (TextView)findViewById(R.id.my_text);


                try {

                    URL url = new URL("http://www.google.com:80/");
                    StringBuilder content = new StringBuilder();
                    // read text returned by server

                    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                    String line;
                    while ((line = in.readLine()) != null) {
                        content.append(line +"\n");
                    }
                    tv.setText(content.toString());
                    setContentView(tv);

                    in.close();
                }

                catch (MalformedURLException e) {

                    System.out.println("Malformed URL: " + e.getMessage());

                }

                catch (IOException e) {

                    System.out.println("I/O Error: " + e.getMessage());

                }


};        


}

解决方案

May be you can try like this, just a thought only :

StringBuilder content = new StringBuilder();
                // read text returned by server
                BufferedReader in = new BufferedReader(new InputStreamReader(new URL("http://www.google.com:80/").openConnection().getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    content.append(line +"\n");
                }

tv.setText(content.toString());
setContentView(tv);

这篇关于不读书的服务器文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆