通过JSON将Android应用程序连接到PHP服务器:套接字异常

时间:2017-11-30 04:39:39

标签: java php android json networking

我试图接收JSON对象并将内容放入ArrayList。我们设置的PHP服务器显示应用程序正在联系服务器,但没有发回任何内容。 我没有收到任何致命错误,但我收到了套接字错误:权限被拒绝。



D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.net.SocketException: Permission denied
W/System.err:     at java.net.Socket.createImpl(Socket.java:454)
W/System.err:     at java.net.Socket.getImpl(Socket.java:517)
W/System.err:     at java.net.Socket.setSoTimeout(Socket.java:1108)
W/System.err:     at com.android.okhttp.Connection.connectSocket(Connection.java:1454)
W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:1413)
W/System.err:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1707)
W/System.err:     at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:133)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:466)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:503)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:247)
W/System.err:     at java.net.URLConnection.getContent(URLConnection.java:737)
W/System.err:     at com.example.steve.plantpals.receiveData.receivePlantList(receiveData.java:41)
W/System.err:     at com.example.steve.plantpals.UserActivity$1.onClick(UserActivity.java:38)
W/System.err:     at android.view.View.performClick(View.java:6205)
W/System.err:     at android.widget.TextView.performClick(TextView.java:11103)
W/System.err:     at android.view.View$PerformClick.run(View.java:23653)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6682)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
W/System.err: java.net.SocketException: Permission denied
W/System.err:     at java.net.Socket.createImpl(Socket.java:454)
W/System.err:     at java.net.Socket.getImpl(Socket.java:517)
W/System.err:     at java.net.Socket.setSoTimeout(Socket.java:1108)
W/System.err:     at com.android.okhttp.Connection.connectSocket(Connection.java:1454)
W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:1413)
W/System.err:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1707)
W/System.err:     at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:133)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:466)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:503)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:247)
W/System.err:     at java.net.URLConnection.getContent(URLConnection.java:737)
W/System.err:     at com.example.steve.plantpals.receiveData.receivePlantList(receiveData.java:41)
W/System.err:     at com.example.steve.plantpals.UserActivity$1.onClick(UserActivity.java:38)
W/System.err:     at android.view.View.performClick(View.java:6205)
W/System.err:     at android.widget.TextView.performClick(TextView.java:11103)
W/System.err:     at android.view.View$PerformClick.run(View.java:23653)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6682)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)




我打电话来接收数据:



public class receiveData {
    int moisture = LocalStorage.moisture;
    User user;

    ArrayList<Plant> receivedPlants = new ArrayList<Plant>();

    URL url = new URL("http://192.168.0.14/steve.php");

   URLConnection urlConnection = (HttpURLConnection) url.openConnection();

    public receiveData() throws IOException {
    }


    public ArrayList<Plant> receivePlantList() {
        // HTTP request
        urlConnection.setDoOutput(true);

        try {
            urlConnection.getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Get items from JSON object and place in a plant list
        BufferedReader reader = null;
        try {
            InputStream stream = urlConnection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            JsonReader jReader = new JsonReader(reader);

            jReader.beginObject();
            while (jReader.hasNext()) {
                jReader.nextName();
                int plantMoisture = jReader.nextInt();
                String plantName = jReader.nextString();
                Plant currentPlant = new Plant(plantName, plantMoisture);
                receivedPlants.add(currentPlant);
            }
            jReader.endObject();
            reader.close();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //jReader.close();
        return receivedPlants;
    }
&#13;
&#13;
&#13;

这是我的Android Manifest:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.steve.plantpals">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.INTERNET" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyPlants"
            android:label="@string/title_activity_my_plants"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".AddPlant" />
        <activity android:name=".testMoistureHigh" />
        <activity android:name=".testMoistureLow" />
        <activity android:name=".testMoistureOptimal" />
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" />
        <activity android:name=".UserActivity"></activity>
    </application>

</manifest>
&#13;
&#13;
&#13;

这是我的第一个需要联网的项目,也是我的第一个stackoverflow问题,很抱歉,如果不清楚的话。

1 个答案:

答案 0 :(得分:0)

您必须在应用程序标记之外声明您的权限

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.INTERNET" />

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    ...