Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java http.getInputStream()返回空值?_Java_Android - Fatal编程技术网

Java http.getInputStream()返回空值?

Java http.getInputStream()返回空值?,java,android,Java,Android,我正在处理这个示例以从web获取图像和文本。但是我得到了一个空指针异常,请帮助我。我已经在清单中添加了所有相关权限。一切看起来都很好 MainActivity.java package com.example.connectweb; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import

我正在处理这个示例以从web获取图像和文本。但是我得到了一个空指针异常,请帮助我。我已经在清单中添加了所有相关权限。一切看起来都很好

MainActivity.java

package com.example.connectweb;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class MainActivity extends Activity {

    ImageView iv;
    TextView edit;
    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.activity_main);
        //Code to download an image
        Bitmap b = WebImage("http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg");
        iv = (ImageView) findViewById(R.id.viewImage);
        iv.setImageBitmap(b);
        String str = WebText("http://techreo.com");
        edit = (TextView) findViewById(R.id.viewText);
        edit.setText(str);
    }

    private String WebText(String string)
    {
        int BUFFER_SIZE = 2000;
        InputStream is = null;
        try {
               is = connect(string);
        }
        catch(Exception e){

            Toast.makeText(getBaseContext(), "Exception is"+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
            return "";
        }
        InputStreamReader inputstr = new InputStreamReader(is);
        int characterReading;
        String s = "";
        char[] inputBuffer = new char[BUFFER_SIZE];
        try {
            while((characterReading = inputstr.read(inputBuffer))>0)
            {
                String readstr = String.copyValueOf(inputBuffer,0, characterReading);
                s += readstr;
                inputBuffer = new char[BUFFER_SIZE];
            }
            is.close();
        }
        catch(Exception e)
        {
            Toast.makeText(this, "Exception is"+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
            return "";
        }
        return s;


    }
    private Bitmap WebImage(String url)
    {
        Bitmap bmp = null;
        InputStream is = null;
        try {
            is = connect(url);
            bmp = BitmapFactory.decodeStream(is);
        }
        catch(Exception e)
        {
            Toast.makeText(getBaseContext(), "Exception is"+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();

        }
        return bmp;
    }

    private InputStream connect(String url) throws IOException
    {
        InputStream is = null;
        int res = -1;
        URL u = new URL(url);
        URLConnection con = u.openConnection();
        if (!(con instanceof HttpURLConnection))
            //throw new ioexception
            Toast.makeText(getBaseContext(), "Not and HTTP Connection::", Toast.LENGTH_SHORT).show();
            try {
                HttpURLConnection http = (HttpURLConnection) con;
                http.setAllowUserInteraction(false);
                http.setInstanceFollowRedirects(true);
                http.setRequestMethod("GET");
                http.connect();
                //System.out.println("InputStream Connected");
                Toast.makeText(getBaseContext(), "InputStream Connected", Toast.LENGTH_SHORT).show();
                res = http.getResponseCode();
                if(res == HttpURLConnection.HTTP_OK)
                {
                      http.setDoInput(true);
                    is = http.getInputStream();
                }


            }
            catch(IOException e){
                e.printStackTrace();
            }
            return is;


    }



}
activitymain.xml

<ScrollView 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"
    tools:context="com.example.connectweb.MainActivity" >

   <LinearLayout 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical"
       >
       <ImageView
           android:id="@+id/viewImage"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="0.46" 
           />
       <TextView
           android:id="@+id/viewText"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="10dp" 
           />
   </LinearLayout>

</ScrollView>

显示

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission 
        android:name="android.permission.INTERNET"

        />
    <uses-permission 
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        />

    <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


如果输入流为空,则表示您有错误,您可以通过记录http.getErrorStream()来获取该错误。

发布日志,NPE发生在哪里?