dongmeng1402 2014-05-14 12:42
浏览 34

Android PHP Get

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 + "
");
            }
        } 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条回答 默认 最新

  • doujiao1814 2014-05-14 12:47
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档