简体   繁体   English

Android,无法从互联网应用接收数据

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

I am completely new to android. 我对android完全陌生。 I am trying to build a basic app to get an idea on "android applications reading data from a server". 我正在尝试构建一个基本的应用程序,以获取关于“ Android应用程序从服务器读取数据”的想法。 To get an idea about http connection on android i created this app watching a video tutorial, i have two java classes. 为了获得有关android上的http连接的想法,我创建了一个观看视频教程的应用程序,我有两个java类。

HttpExample.java class, 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();
    }

}



}

and getmethodhttp.java class, 和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();
            }
        }
    }
}

}

this is my AndroidManifest, 这是我的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>

and my layout xml, 和我的布局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>

now when i run this on emulator or my android phone NOTHING IS DISPLAYED.. Just a blank white layout is visible.. what am i doing wrong here? 现在,当我在模拟器或Android手机上运行此软件时,“什么都没显示”。。。。。。。。。。。。

(i am using eclipse to program) (我正在使用Eclipse进行编程)

First of all you should not change the policy to force network calls on main thread instead use Async task . 首先,您不应更改策略以在主线程上强制进行网络调用,而应使用Async任务

If you want to read html contents then use jsoup which allows to parse html. 如果您想阅读html内容,请使用jsoup ,它可以解析html。 If you want to read data from webservice then try VOLLEY Library.This will save your time,works more efficiently and traceable. 如果要从Web服务读取数据,请尝试VOLLEY Library。这将节省您的时间,工作效率更高且可追溯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过Internet从PC服务器接收Android上的UDP数据 - Unable to receive UDP data on Android from PC server over the Internet Android应用无法连接到互联网 - Android app does not connect to internet SIP注册失败-超时-清单中包含的所有权限,但APP中未包含来自互联网许可的接收数据 - SIP registration fails - TIME OUT - ALL PERMISSIONS included in the manifest BUT the RECEIVE DATA FROM INTERNET PERMISSION not included in the APP 如何在 android 应用程序上从 BLE 设备接收数据 - How to receive data from BLE device on android app Android应用没有从SignalR中心接收数据 - Android app did not receive data from SignalR hub Android 应用程序 - 从服务器发送和接收数据的问题 - Android app - problem with send and receive data from server Android 3.2:接收UDP数据包不起作用? - Android 3.2: Receive UDP packets does not work? 用于我的Android应用程序中的持久数据的Azure移动服务不起作用 - Azure Mobile service for persistent data from my Android app, does not work 套接字不接收数据。 (Android / Java) - Socket does not receive data. (Android/Java) 如何将数据从python脚本通过Internet发送到Android应用程序,反之亦然。 - How to send data from python script through Internet to android app and vice versa.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM