在android中使用php的简单web服务

时间:2016-05-09 10:44:30

标签: android eclipse

    package com.example.simplewebservices;

    import java.io.IOException;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new       HttpPost("http://192.168.0.11/android_connect/first.php");
        try {
           HttpResponse response = httpclient.execute(httppost);
           final String str =  EntityUtils.toString(response.getEntity());
           TextView tv = (TextView) findViewById(R.id.textView1);
           tv.setText(str);
        } 
       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.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);
    }
}

这是activity_main.xml

  <RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.simplewebservices.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" 
        android:text="TextView"/>

    </RelativeLayout>

这里是manifest.xml

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

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <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>

这里有Logcat信息

05-09 15:07:59.726: E/AndroidRuntime(2456): FATAL EXCEPTION: main
05-09 15:07:59.726: E/AndroidRuntime(2456): Process:   com.example.simplewebservices, PID: 2456
05-09 15:07:59.726: E/AndroidRuntime(2456): java.lang.RuntimeException: Unable to start activity    C omponentInfo{com.example.simplewebservices/com.example.simplewebservices.MainActivity}: android.os.NetworkOnMainThreadException
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.os.Looper.loop(Looper.java:135)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.ActivityThread.main(ActivityThread.java:5254)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at java.lang.reflect.Method.invoke(Native Method)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at java.lang.reflect.Method.invoke(Method.java:372)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-09 15:07:59.726: E/AndroidRuntime(2456): Caused by: android.os.NetworkOnMainThreadException
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at libcore.io.IoBridge.connect(IoBridge.java:122)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at java.net.Socket.connect(Socket.java:882)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:124)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:149)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at com.example.simplewebservices.MainActivity.onCreate(MainActivity.java:27)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.Activity.performCreate(Activity.java:5990)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
05-09 15:07:59.726: E/AndroidRuntime(2456):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
05-09 15:07:59.726: E/AndroidRuntime(2456):     ... 10 more
  

`这是使用php.i的android中的简单webservices(hello world)创建php文件并将其复制到wamp server的www目录中。但是当我运行此应用程序时,它很遗憾地停止了。在下面我提到了logcat信息。它如何解决?给我建议..

1 个答案:

答案 0 :(得分:0)

您从主线程调用HTTP连接的问题。您必须在其他线程中调用任何网络操作。

要解决此问题,请使用AsyncTask并对doInBackground方法执行http操作。这解决了这个例外。