1

我试图从网站上阅读文本。我正在使用教程。这是我的java代码

package nl.liacs.team6.stapp;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class Jokes extends Activity {

public void onCreate(Bundle savedInstanceState) {

    try 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jokes);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //dit is t textview element, gevonden door id van de XML layout jokes
        TextView myListView = (TextView)findViewById(R.id.netResult);

        //proberen met internet te connecten
        try{
            //maak nieuw client object
            HttpClient httpclient = new DefaultHttpClient();

            //post to onze url, site is voorbeeld
            HttpPost httppost = new HttpPost("http://ephemeraltech.com/demo/android_tutorial20.php");

            //voor post uit en get response
            HttpResponse response = httpclient.execute(httppost);

            //get message from response
            HttpEntity entity = response.getEntity();

            //get content van message
            InputStream webs = entity.getContent();

            //nu hebben we response, convert to string

            try
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8);

                //lees 1 lijn van text
                myListView.setText(reader.readLine());
                //sluiten
                webs.close();

            }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
            }
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }

}
catch(Exception e){
}

}// onCreate
}// Jokes

这是xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:textIsSelectable="true"
    android:id="@+id/netResult"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

是的,我添加了对 android 清单的权限

当我运行它时什么都没有显示

谁能帮我?我不知道我做错了什么

这是日志

 05-06 10:39:15.501: W/NetworkManagementSocketTagger(887): setKernelCountSet(10012, 0)   failed with errno -2
 05-06 10:39:16.371: I/ActivityManager(887): START {cmp=nl.liacs.team6.stapp/.Info} from pid 1488
 05-06 10:39:16.371: W/WindowManager(887): Failure taking screenshot for (180x300) to layer 21010
 05-06 10:39:16.660: I/ActivityManager(887): Displayed nl.liacs.team6.stapp/.Info: +267ms

太感谢了!!

编辑

 public class Jokes extends Activity {

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.jokes);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //dit is t textview element, gevonden door id van de XML layout jokes
        TextView myListView = (TextView)findViewById(R.id.netResult);
        new loadSomeStuff().execute(FILENAME);      
        }// onCreate

public class loadSomeStuff extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        //proberen met internet te connecten
        try{
            //maak nieuw client object
            HttpClient httpclient = new DefaultHttpClient();

            //post to onze url, site is voorbeeld
            HttpPost httppost = new HttpPost("http://ephemeraltech.com/demo/android_tutorial20.php");

            //voor post uit en get response
            HttpResponse response = httpclient.execute(httppost);

            //get message from response
            HttpEntity entity = response.getEntity();

            //get content van message
            InputStream webs = entity.getContent();

            //nu hebben we response, convert to string

            try
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8);
                //dit is t textview element, gevonden door id van de XML layout jokes
                TextView myListView = (TextView)findViewById(R.id.netResult);
                //lees 1 lijn van text
                myListView.setText(reader.readLine());
                //sluiten
                webs.close();

            }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
            }
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
 return null;
}
 }
 }

我不知道我在做什么以及一切意味着什么,这对我来说都是新的,但感谢你们所有人的帮助。

4

1 回答 1

0

首先,请在清单中添加以下代码

<uses-permission android:name="android.permission.INTERNET"/>

然后将所有网络代码放入线程或异步任务的 doinbackground 方法中。

这将解决您的问题。

您可以使用调用当前异步任务

new loadstuff.execute("url");

public class loadSomeStuff extends AsyncTask<String, Integer, String>{

   String result=null;

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        //proberen met internet te connecten
        try{
            //maak nieuw client object
            HttpClient httpclient = new DefaultHttpClient();

            //post to onze url, site is voorbeeld
            HttpPost httppost = new HttpPost("http://ephemeraltech.com/demo/android_tutorial20.php");

            //voor post uit en get response
            HttpResponse response = httpclient.execute(httppost);

            //get message from response
            HttpEntity entity = response.getEntity();

            //get content van message
            InputStream webs = entity.getContent();

            //nu hebben we response, convert to string

            try
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8);

                //text view removed from here
               result=reader.readline();

                //sluiten
                webs.close();

            }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
            }
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
 return null;
}

 onPostExecute(){

          //declare your text view here & assign result to your text view.
          myTextView.setText(result);

   }
 }
于 2013-05-06T10:48:46.723 回答