Android - InputStreamReader无法正常工作

时间:2014-11-07 15:38:51

标签: java android sdk bufferedreader

我想从网站获取String,并将其转换为Array。它正在eclipse中工作,但如果我想在Android-App中使用它它将无法正常工作。我知道,这肯定是一个愚蠢的错误,但我不知道它在哪里。

InternetHandler:

package de.blender4me.einkaufsliste;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Pattern;

public class internetHandler {

//Einkaufsplaner-API aufrufen
    public static String[] getList(){

        InputStream is = null;

        try
        {
          URL url = new URL("https://einkaufsplaner.blender4me.de/api.php?name=Username&pass=SuperSecretPass&addValue=&removeValue=");
          BufferedReader in = new BufferedReader(
          new InputStreamReader(url.openStream()));

          String Values = null;

          for(int i = 0; i < 4; ++i){
              Values = in.readLine();
          }
          in.close();

          String[] List = Values.split( Pattern.quote( ";" ) );
          return List;

        }
        catch ( Exception e ) {
          e.printStackTrace();
        }
        finally {
          if ( is != null )
            try { is.close(); } catch ( IOException e ) { }
        }
        String[] error = {"error"};
        return error;
    }
}

主要

package de.blender4me.einkaufsliste;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


public class MainActivity extends Activity {

//Widgets
Button addItem;



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

        //listView
        populateListView();

        //register Widgets
        addItem =(Button) findViewById(R.id.addItem);

        addItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                openDialog();
            }
        });
    }

@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);
}

//Alert
public void openDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Zur Einkaufsliste hinzufügen");
    alert.setMessage(" ");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText().toString();
      // Do something with value!
      }
    });

    alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
      }
    });

    alert.show();
}

//listView erstellen
private void populateListView() {
        // Liste auslesen
        String[] values = internetHandler.getList();

        //Liste konvertieren
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                R.layout.listview_item, //Genutztes Layout
                values); //Genutzter Array

        //listView einstellen
        ListView list = (ListView) findViewById(R.id.listView);
        list.setAdapter(adapter);
    }
}

清单

  

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"
     

&GT;                                             

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
     

它总是返回错误数组,但idk为什么:C

logcat的:

  

11-07 15:18:24.425:E / Trace(642):错误打开跟踪文件:没有这样的   文件或目录(2)11-07 15:18:24.925:W / System.err(642):   android.os.NetworkOnMainThreadException 11-07 15:18:24.925:   W / System.err(642):at   android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)   11-07 15:18:24.935:W / System.err(642):at   java.net.InetAddress.lookupHostByName(InetAddress.java:385)11-07   15:18:24.935:W / System.err(642):at   java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)11-07   15:18:24.935:W / System.err(642):at   java.net.InetAddress.getAllByName(InetAddress.java:214)11-07   15:18:24.935:W / System.err(642):at   libcore.net.http.HttpConnection。(HttpConnection.java:70)11-07   15:18:24.935:W / System.err(642):at   libcore.net.http.HttpConnection。(HttpConnection.java:50)11-07   15:18:24.935:W / System.err(642):at   libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:341)   11-07 15:18:24.935:W / System.err(642):at   libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)   11-07 15:18:24.935:W / System.err(642):at   libcore.net.http.HttpConnection.connect(HttpConnection.java:128)11-07   15:18:24.935:W / System.err(642):at   libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)   11-07 15:18:24.945:W / System.err(642):at   libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)   11-07 15:18:24.945:W / System.err(642):at   libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433)   11-07 15:18:24.945:W / System.err(642):at   libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)   11-07 15:18:24.945:W / System.err(642):at   libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)11-07   15:18:24.945:W / System.err(642):at   libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)   11-07 15:18:24.945:W / System.err(642):at   libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)   11-07 15:18:24.945:W / System.err(642):at   libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)   11-07 15:18:24.955:W / System.err(642):at   java.net.URL.openStream(URL.java:462)11-07 15:18:24.955:   W / System.err(642):at   de.blender4me.einkaufsliste.internetHandler.getList(internetHandler.java:21)   11-07 15:18:24.955:W / System.err(642):at   de.blender4me.einkaufsliste.MainActivity.populateListView(MainActivity.java:100)   11-07 15:18:24.955:W / System.err(642):at   de.blender4me.einkaufsliste.MainActivity.onCreate(MainActivity.java:37)   11-07 15:18:24.955:W / System.err(642):at   android.app.Activity.performCreate(Activity.java:5008)11-07   15:18:24.955:W / System.err(642):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)   11-07 15:18:24.955:W / System.err(642):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)   11-07 15:18:24.965:W / System.err(642):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)   11-07 15:18:24.965:W / System.err(642):at   android.app.ActivityThread.access $ 600(ActivityThread.java:130)11-07   15:18:24.965:W / System.err(642):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)   11-07 15:18:24.965:W / System.err(642):at   android.os.Handler.dispatchMessage(Handler.java:99)11-07   15:18:24.965:W / System.err(642):at   android.os.Looper.loop(Looper.java:137)11-07 15:18:24.965:   W / System.err(642):at   android.app.ActivityThread.main(ActivityThread.java:4745)11-07   15:18:24.965:W / System.err(642):at   java.lang.reflect.Method.invokeNative(Native Method)11-07   15:18:24.965:W / System.err(642):at   java.lang.reflect.Method.invoke(Method.java:511)11-07 15:18:24.975:   W / System.err(642):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)   11-07 15:18:24.975:W / System.err(642):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)11-07   15:18:24.975:W / System.err(642):at   dalvik.system.NativeStart.main(Native Method)11-07 15:18:26.455:   D / gralloc_goldfish(642):未检测到GPU仿真的仿真器。

2 个答案:

答案 0 :(得分:0)

问题是您正在尝试在应用程序的UI线程上执行HTTP请求。

您应该在AsyncTask中执行此操作。

答案 1 :(得分:0)

你的问题在这里(在堆栈跟踪中):

android.os.NetworkOnMainThreadException

您正在MainThread中发出请求,该请求禁止使用Android,以避免在UI上进行大量处理(这会阻止用户界面)。

改为使用AsyncTask。

从某事开始的AsyncTask示例:

private class LongOperation extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        InternetHandler ih = new InternetHandler();
        String[] values = ih.getList();
    }

    @Override
    protected void onPostExecute(String result) {
        //Liste konvertieren
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
            R.layout.listview_item, //Genutztes Layout
            values); //Genutzter Array

        //listView einstellen
        ListView list = (ListView) findViewById(R.id.listView);
        list.setAdapter(adapter);
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

用以下方式调用:

LongOperation lo = new LongOperation();
lo.execute();