2
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";
?>

代码中的问题是什么?我正在向 php 文件发送 Http 请求,但没有收到所需的结果。据我说,我认为网址有问题。给我建议

我的日志猫

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)
4

6 回答 6

0

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

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

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

于 2013-08-05T05:54:29.787 回答
0

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

                if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
于 2013-08-05T05:55:30.353 回答
0

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

于 2013-08-05T03:56:41.023 回答
0

使用ipaddress而不是127.0.0.1localhost

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

于 2013-08-05T04:02:57.037 回答
0

你检查你的清单文件,你有没有实现它的 INTERNET 权限?

于 2013-08-05T04:19:48.537 回答
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);
        }
于 2013-08-05T04:30:44.660 回答