下面给出的代码是否有任何错误?

发布于 2024-12-15 11:30:48 字数 2886 浏览 0 评论 0原文

我在运行下面的代码时遇到问题。该代码没有给出错误,但没有给出预期的输出。任何建议或解决方案将受到高度赞赏。提前致谢。

这是我的 xml 文件:-

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

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/readWebpage"
            android:text="Load Webpage"
            android:onClick="readWebpage"/>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/textView1"
              android:text="Example Text"
              android:layout_gravity="center_horizontal"/>

</LinearLayout>

这是我的 java 文件:-

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LoadWebpageAsyncTask extends Activity {

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        tv = (TextView)findViewById(R.id.textView1);

    }
    public class DownloadWebpageTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            // TODO Auto-generated method stub

            String response = "";
            for(String url:urls){
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try{
                    HttpResponse execute = client.execute(httpGet);
                    BufferedReader br = new BufferedReader(new 
                            InputStreamReader(execute.getEntity().getContent()));
                    String s="";
                    while((s = br.readLine()) != null){

                        response += s;
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            return response;
        }


        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            tv.setText(result);
        }

    }

        public void readWebpage(View view){ 
            DownloadWebpageTask task = new DownloadWebpageTask();
            task.execute(new String[]{"http://www.google.com"});

            }
}

I am getting an issue to run the below code.The code gives no error but not giving the expected output. Any suggestions or solution will be highly appreciated.Thanx in advance.

Here's my xml file:-

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

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/readWebpage"
            android:text="Load Webpage"
            android:onClick="readWebpage"/>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/textView1"
              android:text="Example Text"
              android:layout_gravity="center_horizontal"/>

</LinearLayout>

and here's my java file:-

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LoadWebpageAsyncTask extends Activity {

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        tv = (TextView)findViewById(R.id.textView1);

    }
    public class DownloadWebpageTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            // TODO Auto-generated method stub

            String response = "";
            for(String url:urls){
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try{
                    HttpResponse execute = client.execute(httpGet);
                    BufferedReader br = new BufferedReader(new 
                            InputStreamReader(execute.getEntity().getContent()));
                    String s="";
                    while((s = br.readLine()) != null){

                        response += s;
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            return response;
        }


        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            tv.setText(result);
        }

    }

        public void readWebpage(View view){ 
            DownloadWebpageTask task = new DownloadWebpageTask();
            task.execute(new String[]{"http://www.google.com"});

            }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2024-12-22 11:30:48

看起来您在 onCreate()< 中缺少 setContentView(R.layout.<...>);tv 将为 null) /code> 方法并且从不运行您的任务(因此在 tv 上设置文本时不会出现 NullPointerException):

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView)findViewById(R.id.textView1);
    readWebpage(null); //you never use argument anyway
}

edit riha 的答案也是是的,因为你使用它的方式是for (String[] url:urls),而不是 for (String url:urls)

It looks like you are missing setContentView(R.layout.<...>); (tv will be null) in your onCreate() method and never run your task (so you don't get NullPointerException when setting text on tv):

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView)findViewById(R.id.textView1);
    readWebpage(null); //you never use argument anyway
}

edit riha's answer is also true, since the way you use it is for (String[] url:urls), not for (String url:urls)

别在捏我脸啦 2024-12-22 11:30:48

我认为您不需要提供一个字符串数组来执行()。这应该足够了:

task.execute("http://www.google.com");

您可以提供几个 URL,如下所示:

task.execute("url1", "url2");

如果仍然无法正常工作,请进行一些调试:获取 AsyncTask 的代码并将其放入 onCreate() 中。在它上面设置一个断点,然后仔细遍历它,看看有什么问题。

I don't think you need to provide a String array to execute(). That should be sufficient:

task.execute("http://www.google.com");

You could provide several URLs like so:

task.execute("url1", "url2");

If things still don't work, do some debugging: Take the code of your AsyncTask and place it in onCreate(). Put a breakpoint on it and walk through it carefully to see what's wrong with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文