简体   繁体   English

从php接收请求到android

[英]Receiving request from php to android

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

I am not able to receive request from my php file containing 我无法从包含以下内容的php文件中接收请求

<?php
echo "Hello PHP";
?>

What is the problem in the code? 代码有什么问题? I am sending Http request to php file but not receiving the required result. 我正在将Http请求发送到php文件,但未收到所需的结果。 According to me i think there is problem in URL. 根据我的说法,我认为URL中存在问题。 Give me suggestions 给我建议

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

Aamir, try not using the http://127.0.0.1/ URL, from your Android device this will be pointing to Android's localhost which I assume is not where your PHP file is. Aamir,请尝试不使用http://127.0.0.1/ URL,从您的Android设备上,这将指向Android的localhost,我认为这不是您的PHP文件所在的位置。 If the device and php server are on the same local network use your local IP address instead. 如果设备和PHP服务器在同一本地网络上,请使用您的本地IP地址。

Use ipaddress instead of 127.0.0.1 or localhost . 使用ipaddress而不是127.0.0.1localhost

in command prompt type ipconfig and get your ipaddress . 在命令提示符下,键入ipconfig并获取您的ipaddress

您是否检查了清单文件,是否已经实现了INTERNET权限?

use ip address 10.0.2.2 instead of 127.0.0.1 使用ip address 10.0.2.2 instead of 127.0.0.1

so try by changing to 因此请尝试更改为

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

PS. PS。

either use AysncTask 要么使用AysncTask

or add following code in your onCreate() after setContentView(R.layout.activity_main); 或在setContentView(R.layout.activity_main);之后的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);
        }

Your problem is your performing network activity on the main thread. 您的问题是您在主线程上执行网络活动。

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

This type of behavior can lead to ANR (Activity Not responding) because the UI thread gets blocked with I/O activity. 这种类型的行为可能导致ANR(活动无响应),因为UI线程被I / O活动阻塞了。 Your suggested to use AsyncTask to perform this. 您建议使用AsyncTask执行此操作。

if you are using higher sdk version used this following code 如果您使用的是更高版本的SDK,请使用以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM