4
我想訪問一個網站,並把HTML源代碼到TextView中。我的模擬器上的瀏覽器正常工作,但我相信在我的應用程序中訪問互聯網的功能不正常。我想我可能不得不使用AsyncTask,但我不知道如何正確實現,或者我甚至不得不使用它。我對編程非常陌生,並且一直在這個網站上尋找一段時間,但似乎無法弄清楚。HttpClient連接到互聯網;錯誤打開跟蹤文件:沒有這樣的文件或目錄(2)
這是主代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView httpTest = (TextView) findViewById(R.id.textView1);
GetTest sourceCode = new GetTest();
String returned;
try {
returned = sourceCode.getInternetData();
httpTest.setText(returned);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
這裏是應該得到的HTML源代碼的代碼:
public class GetTest {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI(
"http://www.yahoo.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null) {
try {
in.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
我的清單中的權限:
<uses-permission android:name="android.permission.INTERNET" />
最後,這裏是我的logcat:
04-30 02:28:44.368: E/Trace(1095): error opening trace file: No such file or directory (2)
04-30 02:28:45.338: D/dalvikvm(1095): GC_FOR_ALLOC freed 51K, 7% free 2555K/2728K, paused 41ms, total 44ms
04-30 02:28:45.348: I/dalvikvm-heap(1095): Grow heap (frag case) to 3.216MB for 635812-byte allocation
04-30 02:28:45.398: D/dalvikvm(1095): GC_FOR_ALLOC freed 2K, 6% free 3174K/3352K, paused 45ms, total 45ms
04-30 02:28:45.447: D/dalvikvm(1095): GC_CONCURRENT freed <1K, 6% free 3177K/3352K, paused 5ms+4ms, total 57ms
04-30 02:28:45.509: W/System.err(1095): android.os.NetworkOnMainThreadException
04-30 02:28:45.509: W/System.err(1095): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-30 02:28:45.509: W/System.err(1095): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-30 02:28:45.509: W/System.err(1095): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-30 02:28:45.509: W/System.err(1095): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-30 02:28:45.518: W/System.err(1095): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-30 02:28:45.518: W/System.err(1095): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-30 02:28:45.518: W/System.err(1095): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-30 02:28:45.518: W/System.err(1095): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-30 02:28:45.518: W/System.err(1095): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-30 02:28:45.528: W/System.err(1095): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-30 02:28:45.528: W/System.err(1095): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-30 02:28:45.528: W/System.err(1095): at com.example.edittexttest.GetTest.getInternetData(GetTest.java:24)
04-30 02:28:45.528: W/System.err(1095): at com.example.edittexttest.MainActivity.onCreate(MainActivity.java:18)
04-30 02:28:45.528: W/System.err(1095): at android.app.Activity.performCreate(Activity.java:5104)
04-30 02:28:45.528: W/System.err(1095): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-30 02:28:45.528: W/System.err(1095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-30 02:28:45.528: W/System.err(1095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-30 02:28:45.528: W/System.err(1095): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-30 02:28:45.528: W/System.err(1095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-30 02:28:45.538: W/System.err(1095): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 02:28:45.538: W/System.err(1095): at android.os.Looper.loop(Looper.java:137)
04-30 02:28:45.538: W/System.err(1095): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-30 02:28:45.538: W/System.err(1095): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 02:28:45.538: W/System.err(1095): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 02:28:45.538: W/System.err(1095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-30 02:28:45.538: W/System.err(1095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-30 02:28:45.548: W/System.err(1095): at dalvik.system.NativeStart.main(Native Method)
04-30 02:28:45.758: D/gralloc_goldfish(1095): Emulator without GPU emulation detected.
感謝您的高級幫助!
謝謝!我試圖給你一個upvote,但我沒有足夠的代表去做。一個額外的問題:我的doInBackGround返回一個錯誤。它說「這個方法必須返回字符串類型的結果」。我相信這是因爲我的catch語句沒有返回任何東西。爲了解決這個問題,我聲明瞭一個任意的String變量,並在catch語句中返回它。如果try語句有效,這應該不成問題,但是有沒有辦法通過返回實際的「返回」字符串變量來解決這個錯誤? – 2013-05-01 01:11:45
是的你是對的。你必須返回返回...給我1分鐘。我在編輯答案。 (我修改了doInBackground)請參閱更新。 – pt2121 2013-05-01 01:55:21
完美的作品。你搖滾 – 2013-05-01 03:14:18