下面给出的代码是否有任何错误?
我在运行下面的代码时遇到问题。该代码没有给出错误,但没有给出预期的输出。任何建议或解决方案将受到高度赞赏。提前致谢。
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您在
onCreate()< 中缺少
setContentView(R.layout.<...>);
(tv
将为 null) /code> 方法并且从不运行您的任务(因此在tv
上设置文本时不会出现NullPointerException
):edit riha 的答案也是是的,因为你使用它的方式是
for (String[] url:urls)
,而不是for (String url:urls)
It looks like you are missing
setContentView(R.layout.<...>);
(tv
will be null) in youronCreate()
method and never run your task (so you don't getNullPointerException
when setting text ontv
):edit riha's answer is also true, since the way you use it is
for (String[] url:urls)
, notfor (String url:urls)
我认为您不需要提供一个字符串数组来执行()。这应该足够了:
您可以提供几个 URL,如下所示:
如果仍然无法正常工作,请进行一些调试:获取 AsyncTask 的代码并将其放入 onCreate() 中。在它上面设置一个断点,然后仔细遍历它,看看有什么问题。
I don't think you need to provide a String array to execute(). That should be sufficient:
You could provide several URLs like so:
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.