Android应用程序 - http不会起作用

时间:2014-07-09 16:43:57

标签: android http

我的清单文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.biscani.alpha"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <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=".MainActivity"
            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>

这是我的主要活动:

    package net.biscani.alpha;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

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

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView list = (TextView)findViewById(R.id.netResult);

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://ephemeraltech.com/demo/android_tutorial20.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream webs = entity.getContent();

            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
                list.setText(reader.readLine());
                webs.close();
            }catch(Exception e) {
                Log.e("log_tag", "Error converting result "+e.toString());
            }
        }catch(Exception e) {
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
    }catch(Exception e) {
        Log.e("ERROR", "Error in code "+e.toString());
        e.printStackTrace();
    }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的布局main.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.biscani.alpha.MainActivity" >

    <TextView
        android:id="@+id/netResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

它没有显示我在http帖子中请求的网站上的文字,它只是在屏幕上显示为空白。此外,我如何解析来自具有图像和其他链接的网站的数据以及数据,如博客文章等?

07-09 19:00:59.464:E / log_tag(20792):http连接错误android.os.NetworkOnMainThreadException

提前致谢。

2 个答案:

答案 0 :(得分:1)

您正在onCreate()中运行网络操作,该操作在UI线程上运行。

您应该在新的线程中运行您的代码,然后它才能运行。

查看类:Thread,Runnable,Handler,AsyncTask,AsyncTaskLoader

有关UI线程及其不应被“阻止”的信息,请参阅此链接,还将使用AsyncTask在单独的线程中演示正在运行的代码

https://developer.android.com/training/articles/perf-anr.html#Avoiding

答案 1 :(得分:0)

AsyncTask类中使用您的http请求(非UI线程),并在onpostExecute()(UI线程)中获取结果