簡體   English   中英

Android,無法從互聯網應用接收數據

[英]Android, receive data from internet app does not work

我對android完全陌生。 我正在嘗試構建一個基本的應用程序,以獲取關於“ Android應用程序從服務器讀取數據”的想法。 為了獲得有關android上的http連接的想法,我創建了一個觀看視頻教程的應用程序,我有兩個java類。

HttpExample.java類,

package com.raveen.testingthree;

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

public class HttpExample extends Activity {

TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .detectAll().penaltyLog().build();
    StrictMode.setThreadPolicy(policy);
    super.onCreate(savedInstanceState);
    httpStuff = (TextView) findViewById(R.id.tvHttp);
    GetMethodHttp test = new GetMethodHttp();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



}

和getmethodhttp.java類,

package com.raveen.testingthree;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

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

public class GetMethodHttp {

public String getInternetData () throws Exception{
    BufferedReader in = null;
    String data = null;

    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.google.com");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse responce = client.execute(request);

        in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String newLine = System.getProperty("line.separator");

        while((line = in.readLine()) != null){
            sb.append(line + newLine);

        }
        in.close();
        data = sb.toString();
        return data;

    } finally {
        if (in != null){
            try{
                in.close();
                return data;
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

}

這是我的AndroidManifest,

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HttpExample"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>


</manifest>

和我的布局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" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/tvHttp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading Data"
        android:textSize="20dp"
         />
</ScrollView>

</LinearLayout>

現在,當我在模擬器或Android手機上運行此軟件時,“什么都沒顯示”。。。。。。。。。。。。

(我正在使用Eclipse進行編程)

首先,您不應更改策略以在主線程上強制進行網絡調用,而應使用Async任務

如果您想閱讀html內容,請使用jsoup ,它可以解析html。 如果要從Web服務讀取數據,請嘗試VOLLEY Library。這將節省您的時間,工作效率更高且可追溯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM