在Android中使用Wi-Fi时出现FileNotFoundException

时间:2016-02-15 10:13:26

标签: java android wireless

当我使用我的数据计划并且我的应用程序工作正常时,我的脚本具有完全访问权限,只有当我切换到Wi-Fi时,我的脚本才会出现500内部服务器错误。

我正在使用php脚本,我的应用程序使用:

访问它
    public JSONObject getHttpResponse(String domUrl){

    String parsedString = "";

    //Log.d("Tag Name", "gggg");
    try {

        URL url = new URL(domUrl);
        URLConnection conn = url.openConnection();

        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        InputStream is = httpConn.getInputStream();
        parsedString = convertinputStreamToString(is);

        httpConn.disconnect();
        //((HttpURLConnection) conn).disconnect();

        if(parsedString.length() > 0){
            return getJSONResponseObject(parsedString);
        }

        //Log.d("Tag Name xfgfgxfgxfg", parsedString);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

我的AndroidManifest.xml文件包含:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.towntour.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/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>

我得到的错误:

java.io.FileNotFoundException: http://www.hugt.co.uk/getTopThreads.php?threadTitle=&threadCountry=&threadCategory=&threadSubCategory=
    02-15 10:28:10.675    6161-6184/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
    02-15 10:28:10.675    6161-6184/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.threadbump.trikampatel.threadbump.MainActivity.getHttpResponse(MainActivity.java:1220)
    02-15 10:28:10.675    6161-6184/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.threadbump.trikampatel.threadbump.MainActivity$17.run(MainActivity.java:1063)
    02-15 10:28:10.675    6161-6184/com.threadbump.trikampatel.threadbump W/System.err﹕ at java.lang.Thread.run(Thread.java:864)
    02-15 10:28:10.685    6161-6183/com.threadbump.trikampatel.threadbump W/System.err﹕ java.io.FileNotFoundException: http://www.hugt.co.uk/getFavourites.php?userID=49&threadTitle=&threadCountry=&threadCategory=&threadSubCategory=
    02-15 10:28:10.685    6161-6183/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
    02-15 10:28:10.685    6161-6183/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.threadbump.trikampatel.threadbump.MainActivity.getHttpResponse(MainActivity.java:1220)
    02-15 10:28:10.685    6161-6183/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.threadbump.trikampatel.threadbump.MainActivity$16.run(MainActivity.java:1009)
    02-15 10:28:10.685    6161-6183/com.threadbump.trikampatel.threadbump W/System.err﹕ at java.lang.Thread.run(Thread.java:864)
    02-15 10:28:10.685    6161-6182/com.threadbump.trikampatel.threadbump W/System.err﹕ java.io.FileNotFoundException: http://www.hugt.co.uk/getThread.php?threadTitle=&threadCountry=&threadCategory=&threadSubCategory=
    02-15 10:28:10.685    6161-6182/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
    02-15 10:28:10.685    6161-6182/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.threadbump.trikampatel.threadbump.MainActivity.getHttpResponse(MainActivity.java:1220)
    02-15 10:28:10.685    6161-6182/com.threadbump.trikampatel.threadbump W/System.err﹕ at com.threadbump.trikampatel.threadbump.MainActivity$12.run(MainActivity.java:714)
    02-15 10:28:10.685    6161-6182/com.threadbump.trikampatel.threadbump W/System.err﹕ at java.lang.Thread.run(Thread.java:864)

有人可以提出建议吗?

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题。

添加此行

   urlConnection.setRequestProperty("Accept-Encoding", "identity");

设置Accept-Encoding请求标头显式禁用自动解压缩并保持响应头完整无缺;根据响应的Content-Encoding标头,调用者必须根据需要处理解压缩。通过android HttpUrlconnection引用http://developer.android.com/reference/java/net/HttpURLConnection.html