0

Here is my LogCat...
Pls explain it for a noob, this is my first app program... But i need it for my Homework...
Pls help me i need to get the echo of a php script and handle this data

05-14 14:03:22.268: E/AndroidRuntime(4674): FATAL EXCEPTION: main
05-14 14:03:22.268: E/AndroidRuntime(4674): java.lang.IllegalStateException: Could not execute method of the activity
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.view.View$1.onClick(View.java:3063)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.view.View.performClick(View.java:3534)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.view.View$PerformClick.run(View.java:14263)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.os.Handler.handleCallback(Handler.java:605)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.os.Looper.loop(Looper.java:137)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.app.ActivityThread.main(ActivityThread.java:4441)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.lang.reflect.Method.invoke(Method.java:511)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at dalvik.system.NativeStart.main(Native Method)
05-14 14:03:22.268: E/AndroidRuntime(4674): Caused by: java.lang.reflect.InvocationTargetException
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.lang.reflect.Method.invoke(Method.java:511)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.view.View$1.onClick(View.java:3058)
05-14 14:03:22.268: E/AndroidRuntime(4674):     ... 11 more
05-14 14:03:22.268: E/AndroidRuntime(4674): Caused by: android.os.NetworkOnMainThreadException
05-14 14:03:22.268: E/AndroidRuntime(4674):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-14 14:03:22.268: E/AndroidRuntime(4674):     at com.me.bittwaldstream.MainActivity.sendLogin(MainActivity.java:163)
05-14 14:03:22.268: E/AndroidRuntime(4674):     ... 14 more

I dont know what to do... Im confused... This is my first Programm with Android and Java but i need it for my homework... (I know it will look like shit :))

Here is my full ActivityMain.java

package com.me.bittwaldstream;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new LoginFragment())
                    .commit();
        } 
    }


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

    public static class LoginFragment extends Fragment {

        public LoginFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

    }

    public static class WaitFragment extends Fragment {

        public WaitFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.waiting_main, container, false);
            return rootView;
        }

    }

    public static class StreamFragment extends Fragment {

        public StreamFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.videostream_main, container, false);
            return rootView;
        }

    }

    private static String readStream(InputStream is) {

        final String TAG = "Stream";

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            Log.e(TAG, "IOException", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException", e);
            }
        }
        return sb.toString();
    }

    //App beenden
    public void abortApp(View v) {
        finish();
    }

    //Login prüfen
    public void sendLogin(View v) {
        /*getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, new WaitFragment())
                    .commit();*/

        EditText username;
        EditText passwort;
        String result = "";
        username = (EditText) findViewById(R.id.editTextUsername);
        passwort = (EditText) findViewById(R.id.editTextPasswort);

        if(username.getText().toString().trim().length() <= 0)
        {
            Toast.makeText(this, "Bitte geben Sie einen Usernamen an", Toast.LENGTH_LONG).show();
        }

        if(passwort.getText().toString().trim().length() <= 0)
        {
            Toast.makeText(this, "Bitte geben Sie ihr Passwort an", Toast.LENGTH_LONG).show();
        }

        URL url;
        try {
            url = new URL("http://shop.kokakukidotai.cf/echotest.php");
            HttpURLConnection mUrlConnection = (HttpURLConnection) url.openConnection();
            mUrlConnection.setDoInput(true);
            InputStream is = new BufferedInputStream(mUrlConnection.getInputStream());
            result = readStream(is);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
        }

        if(result == "allow")
        {
            Toast.makeText(this, "Login erfolgreich", Toast.LENGTH_LONG).show();
        }
    }

    public void acceptRequest(View v) {
        getSupportFragmentManager().beginTransaction()
        .replace(R.id.container, new StreamFragment())
        .commit();
    }

    public void overjump(View v) {
        Button AcceptEvent;
        ProgressBar Wait;
        TextView Message;

        AcceptEvent = (Button) findViewById(R.id.buttonAccept);
        Wait = (ProgressBar) findViewById(R.id.progressBarWait);
        Message = (TextView) findViewById(R.id.textViewWaiting);

        Wait.setVisibility(View.GONE);
        AcceptEvent.setVisibility(View.VISIBLE);
        Message.setText(R.string.accept_event);
    }

}

Here are my Mainfest.xml:

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

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

    <uses-permission  android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <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="com.me.bittwaldstream.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>
1
  • 1
    I'll give you a hint: You have to use AsyncTask when fetching data from internet in your app. Commented May 14, 2014 at 12:47

1 Answer 1

2

You are throwing a NetworkOnMainThreadException by performing network operations on your main Activity.

This is in place since Ice Cream Sandwitch.

Use an AsyncTask, Service, Handler, or in general, a background Thread to perform those operations.

Sign up to request clarification or add additional context in comments.

3 Comments

I dont know what it mean.. Thats my first "programm" as app and in Java
@user3636558 you need to take a look at threading operations for Android. Specifically the operations permitted on the main or UI thread. I've edited my answer with a couple of links to the API. I recommend you get your hands dirty with AsyncTask in this case.
Jeha AsyncTask ... I read some about it but its to difficult to understand for me... Its my first project. I never worked with java or the sdk before. As example i dont know how to call functions out of other files if i would create some...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.