接收从php到android的请求

时间:2013-08-05 03:38:38

标签: php android wamp

package com.sunil.phpconnect;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    HttpClient httpclient;
    HttpGet request;
    HttpResponse response;
    String url;

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

        // URL of PHP Script

        url = "http://192.168.3.103/index.php";

        // TextView to display result

        TextView result = (TextView) findViewById(R.id.tvResult);

        // Try to connect using Apache HttpClient Library
        try {
            httpclient = new DefaultHttpClient();
            request = new HttpGet(url);
            response = httpclient.execute(request);
            }

        catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "First Exception", Toast.LENGTH_LONG).show();
        }

        // response code
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {

                // Appending result to textview
                result.append(line);
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Second Exception", Toast.LENGTH_LONG).show();
        }
    }
}

我无法接收来自包含

的php文件的请求
<?php
echo "Hello PHP";
?>

代码中有什么问题? 我发送Http请求到PHP文件但没有收到所需的结果。 根据我的想法,我认为URL存在问题。 给我建议

我的LOGCAT

08-05 05:07:30.311: W/System.err(2009): android.os.NetworkOnMainThreadException
08-05 05:07:30.321: W/System.err(2009):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-05 05:07:30.331: W/System.err(2009):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-05 05:07:30.341: W/System.err(2009):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-05 05:07:30.341: W/System.err(2009):     at libcore.io.IoBridge.connect(IoBridge.java:112)
08-05 05:07:30.341: W/System.err(2009):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-05 05:07:30.341: W/System.err(2009):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-05 05:07:30.341: W/System.err(2009):     at java.net.Socket.connect(Socket.java:842)
08-05 05:07:30.341: W/System.err(2009):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-05 05:07:30.341: W/System.err(2009):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-05 05:07:30.341: W/System.err(2009):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-05 05:07:30.361: W/System.err(2009):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-05 05:07:30.361: W/System.err(2009):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-05 05:07:30.361: W/System.err(2009):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-05 05:07:30.361: W/System.err(2009):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-05 05:07:30.361: W/System.err(2009):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-05 05:07:30.361: W/System.err(2009):     at com.sunil.phpconnect.MainActivity.onCreate(MainActivity.java:40)
08-05 05:07:30.381: W/System.err(2009):     at android.app.Activity.performCreate(Activity.java:5104)
08-05 05:07:30.381: W/System.err(2009):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-05 05:07:30.381: W/System.err(2009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-05 05:07:30.381: W/System.err(2009):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-05 05:07:30.381: W/System.err(2009):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-05 05:07:30.381: W/System.err(2009):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-05 05:07:30.381: W/System.err(2009):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 05:07:30.381: W/System.err(2009):     at android.os.Looper.loop(Looper.java:137)
08-05 05:07:30.381: W/System.err(2009):     at android.app.ActivityThread.main(ActivityThread.java:5039)
08-05 05:07:30.381: W/System.err(2009):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 05:07:30.381: W/System.err(2009):     at java.lang.reflect.Method.invoke(Method.java:511)
08-05 05:07:30.393: W/System.err(2009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 05:07:30.401: W/System.err(2009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 05:07:30.401: W/System.err(2009):     at dalvik.system.NativeStart.main(Native Method)

6 个答案:

答案 0 :(得分:0)

阿米尔, 尽量不要使用 http://127.0.0.1/网址,来自您的Android设备,这将指向Android的localhost,我认为这不是您的PHP文件所在。如果设备和php服务器在同一本地网络上,请改用本地IP地址。

答案 1 :(得分:0)

使用 ipaddress 代替 127.0.0.1 localhost

在命令提示符下键入 ipconfig 并获取 ipaddress

答案 2 :(得分:0)

您是否检查过清单文件,是否已为其实施INTERNET权限?

答案 3 :(得分:0)

使用ip address 10.0.2.2 instead of 127.0.0.1

所以尝试改为

url = "http://10.0.2.2/index.php";

<强> PS。

使用AysncTask

或在onCreate()

之后的setContentView(R.layout.activity_main);中添加以下代码
if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

答案 4 :(得分:0)

您的问题是您在主线程上执行的网络活动。

08-05 05:07:30.311: W/System.err(2009): android.os.NetworkOnMainThreadException

此类行为可能导致ANR(活动未响应),因为UI线程被I / O活动阻止。您的建议使用AsyncTask来执行此操作。

答案 5 :(得分:0)

如果您使用更高版本的sdk,请使用以下代码

                if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }