繁体   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