1

运行此代码时,我的应用程序崩溃:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_biografie);
        setTitle("Biografie");

        try {
            // Create a URL for the desired page
            URL url = new URL("http://xxx.nl/api/biografie.php?dataid=998");

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
                TextView text = (TextView)findViewById(R.id.biografieText);
                text.setText(str);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
    }

这是我的 Logcat 输出:

I/Process(30127):发送信号。PID:30127 SIG:9

我该怎么做才能将网络上的文本放入 TextView?这是一个 PHP 脚本,输出带有传记。

在此致谢!

4

3 回答 3

1

这将是一个更好的做法,使用异步任务

AsyncronousTask atask = new AsyncronousTask();          
atask.execute("ur url here");
String res=atask.get();


//Asyncronous taks class goes like

public class AsyncronousTask extends AsyncTask<String, Void, String> {
String response;
Activity c;
//private ProgressDialog dialog;
//String title,msg;


@Override
protected String doInBackground(String... urls) {
    response = "";
    for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse execute = client.execute(httpGet);
            InputStream content = execute.getEntity().getContent();
            BufferedReader buffer = new BufferedReader( new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {
                response += s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return response;
}

}
于 2013-03-26T11:51:41.413 回答
0

使用此代码,您将获得响应以返回此方法调用,然后您可以在 textView 上设置它

公共静态字符串 HitServerGET(字符串 URL){

    String result = null;

    InputStream is = null;

    StringBuilder sb = null;

    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
    }

    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line = "0";

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        result = sb.toString();

    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    return result;
于 2013-03-26T11:44:25.003 回答
0

在 onPostExecute 中,您可以在 TextView 中设置文本。
我重用了 Saad Khokhar 的 HitServerGET 函数

这段代码可以放在你的 onCreate 中。

final URL url = new URL("http://xxx.nl/api/biografie.php?dataid=998");
new AsyncTask<String, String, String>()
{
  String text=null;         
  @Override
  protected String doInBackground(String... params)
  {
    text = HitServerGET(url)
  }

  @Override
  protected void onPostExecute(String ret)
  {
    ((TextView)findViewById(R.id.biografieText)).setText(text);
  }
}.execute();
于 2013-03-26T11:57:27.193 回答