0

だから私はアプリを作っています、私は初心者ですが、行き詰まっています、私はこのコードを持っています:

public class MenuChoice extends Activity {
    InputStream string = null;
    ArrayList<String> Deals = null;
    ArrayList<String> ImageBase;
    int currentDeal = 0;

    public void StreamReader() throws IOException{
        InputStreamReader is = new InputStreamReader(string);
        StringBuilder sb=new StringBuilder();
        BufferedReader br = new BufferedReader(is);
        String read = br.readLine();

        while(read != null) {
            //System.out.println(read);
            sb.append(read);
            read =br.readLine();

        }
        String[] DealList = sb.toString().split("±");
        //ArrayList<String> deals = new ArrayList<String>();
        for(int i = 0; i < DealList.length; i++){
            Log.d("msgs", DealList[i]);
            if(DealList[i].length() > 5){
                Deals.add(DealList[i]);
            }
        }
//      return deals;
    }
    public String DownloadImg(String urla, String newname){
        String fileName = null;
        try{
        URL url = new URL (urla);
        InputStream input = url.openStream();
        try {
        //The sdcard directory e.g. '/sdcard' can be used directly, or 
        //more safely abstracted with getExternalStorageDirectory()
        File storagePath = Environment.getExternalStorageDirectory();
        OutputStream output = new FileOutputStream (storagePath + "/" + newname + "/.png");
        fileName = storagePath + "/" + newname + "/.png";
        try {
            byte[] buffer = new byte[5000];
            int bytesRead = 0;
            while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                output.write(buffer, 0, bytesRead);
            }
        } finally {
            output.close();
        }
        } finally {
        input.close();
        }
        }
        catch(Exception e){
        }

        return fileName;
    }
    public void SetInp(InputStream inp){
        string = inp;
    }

    public class GetDeals extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            InputStream str = null;
            HttpClient htc = new DefaultHttpClient();
            HttpPost htp = new HttpPost("site/android_connect.php");
                try {
                    HttpEntity resp = htc.execute(htp).getEntity();
                    InputStream strings = resp.getContent();
                    str = strings;
                } catch (ClientProtocolException e) {
                    Log.d("problema" ,"BAD!");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d("problema" ,"BAD!");
                    e.printStackTrace();
                }
            string = str;
            return null;

    }
    }


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

        try {
            new GetDeals().execute().get();
        }
        catch(Exception e){}
        /*    try {
            Deals = StreamReader(string);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*
        for(int i = 0; i<Deals.size(); i++){

            ImageBase.add(DownloadImg(Deals.get(i), "Deal"+i));
            Log.d("bugger", DownloadImg(Deals.get(i), "Deal"+i));
        }
        */
        try {
            StreamReader();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            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.menu_choice, menu);
        return true;
    }

}

だから私はこのエラーがスローされます:

08-26 17:42:52.030: E/AndroidRuntime(10221): 致命的な例外: メイン 08-26 17:42:52.030: E/AndroidRuntime(10221): java.lang.RuntimeException: アクティビティ ComponentInfo{plugMedia. kcrarea/plugMedia.kcrarea.MenuChoice}: android.os.NetworkOnMainThreadException 08-26 17:42:52.030: E/AndroidRuntime(10221): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 08-26 17: 42:52.030: E/AndroidRuntime(10221): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 08-26 17:42:52.030: E/AndroidRuntime(10221): android.app.ActivityThread.access で$600(ActivityThread.java:123) 08-26 17:42:52.030: E/AndroidRuntime(10221): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 08-26 17:42:52.030: E/AndroidRuntime(10221): android.os.Handler で。dispatchMessage(Handler.java:99) 08-26 17:42:52.030: E/AndroidRuntime(10221): android.os.Looper.loop(Looper.java:137) 08-26 17:42:52.030: E/ AndroidRuntime(10221): android.app.ActivityThread.main(ActivityThread.java:4424) 08-26 17:42:52.030: E/AndroidRuntime(10221): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) で08-26 17:42:52.030: E/AndroidRuntime(10221): java.lang.reflect.Method.invoke(Method.java:511) 08-26 17:42:52.030: E/AndroidRuntime(10221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 08-26 17:42:52.030: E/AndroidRuntime(10221): com.android.internal.os.ZygoteInit.main(ZygoteInit) .java:554) 08-26 17:42:52.030: E/AndroidRuntime(10221): dalvik.system.NativeStart.main(ネイティブ メソッド) 08-26 17:42:52.030: E/AndroidRuntime(10221): 原因に:android.os.NetworkOnMainThreadException 08-26 17:42:52.030: E/AndroidRuntime(10221): android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 08-26 17:42:52.030: E/AndroidRuntime (10221): libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 08-26 17:42:52.030: E/AndroidRuntime(10221): libcore.io.IoBridge.recvfrom(IoBridge.java:503) で08-26 17:42:52.030: E/AndroidRuntime(10221): java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) で 08-26 17:42:52.030: E/AndroidRuntime(10221): Java で。 net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 08-26 17:42:52.030: E/AndroidRuntime(10221): java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 08-26 17 :42:52.030: E/AndroidRuntime(10221): org.apache.http.impl.io.AbstractSessionInputBuffer で。fillBuffer(AbstractSessionInputBuffer.java:103) 08-26 17:42:52.030: E/AndroidRuntime(10221): org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:120) 08-26 17: 42:52.030: E/AndroidRuntime(10221): org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:211) 08-26 17:42:52.030: E/AndroidRuntime(10221): org .apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:183) 08-26 17:42:52.030: E/AndroidRuntime(10221): org.apache.http.impl.io.ChunkedInputStream.read( ChunkedInputStream.java:155) 08-26 17:42:52.030: E/AndroidRuntime(10221): org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) 08-26 17:42:52.030: E/AndroidRuntime(10221): java.io.InputStreamReader.read(InputStreamReader.java:244) 08-26 17:42:52 で。030: E/AndroidRuntime(10221): java.io.BufferedReader.fillBuf(BufferedReader.java:130) 08-26 17:42:52.030: E/AndroidRuntime(10221): java.io.BufferedReader.readLine(BufferedReader) .java:390) 08-26 17:42:52.030: E/AndroidRuntime(10221): plugMedia.kcrarea.MenuChoice.StreamReader(MenuChoice.java:59) 08-26 17:42:52.030: E/AndroidRuntime(10221) ): plugMedia.kcrarea.MenuChoice.onCreate(MenuChoice.java:158) 08-26 17:42:52.030: E/AndroidRuntime(10221): android.app.Activity.performCreate(Activity.java:4465) 08- 26 17:42:52.030: E/AndroidRuntime(10221): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 08-26 17:42:52.030: E/AndroidRuntime(10221): android.app. ActivityThread.performLaunchActivity(ActivityThread.java:1920) 08-26 17:42:52.030: E/AndroidRuntime(10221): ...11以上

メインスレッドでネットワークアクションを実行しようとしていると言っているのは理解していますが、私がしているのは StreamReader を呼び出しているだけで、InputStream を変換するだけだと思います (AsyncTask から保存して取得しましたが、エラーがスローされます。

何か助けはありますか?何が原因ですか?

ありがとうございました。

4

1 に答える 1