【问题标题】:Android, receive data from internet app does not workAndroid,从互联网应用程序接收数据不起作用
【发布时间】:2015-05-17 04:26:47
【问题描述】:

我对 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>

现在,当我在模拟器或我的安卓手机上运行它时,什么都没有显示。只有一个空白的白色布局是可见的。我在这里做错了什么?

(我正在使用eclipse编程)

【问题讨论】:

  • 查看 Logcat 时,是否有任何堆栈跟踪被打印出来?
  • log cat 中有很多行打印出来。哪些是堆栈跟踪?例外?
  • 他们将是例外。它可能看起来像“com.example.activity 第 145 行的 NullPointerException at com.example... 它可能会告诉我们代码中出了什么问题。
  • 哦,是的,在许多带有标签 system.err 的橙色线中都有一条红线。 .有一个异常unknownHostException!!说没有与主机名关联的地址..但我也尝试了 www.google.com,但仍然出现异常..为什么?
  • 那些仅在我使用 Phone 时打印,但当我使用模拟器时,这些错误都不会打印。有一个 nullpointerexception.. 但仍然没有显示任何内容

标签: java android eclipse androidhttpclient


【解决方案1】:

首先,您不应更改策略以强制在主线程上进行网络调用,而应使用Async task

如果您想阅读 html 内容,请使用jsoup,它允许解析 html。 如果您想从 Web 服务读取数据,请尝试 VOLLEY 库。这将节省您的时间,工作效率更高且可追溯。

【讨论】:

    猜你喜欢
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2019-06-22
    相关资源
    最近更新 更多