我的http POST请求在模拟器和真实设备上都能正常工作。在我的几个朋友中,http请求仅适用于移动数据。这个朋友有一个“华为荣誉X6”。他很远,所以我不能在android studio中用他的手机调试。
我得到的唯一信息是,在WIFI上,该请求引发了IOException:
e.getLocalizedMessage():http://134.256.74.200/FactFolder/sendFact.php
e.fillInStackTrace():java.io.FileNotFoundException:http://134.256.74.200/FactFolder/sendFact.php
我真的不明白为什么在移动数据上一切正常,但在WIFI上却无法正常工作。 是因为手机型号吗?
清单:
<?xml version="1.0" encoding="utf-8"?>
<!-- Include required permissions for Google Mobile Ads to run. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/logo_blue"
android:label="@string/app_name"
android:roundIcon="@drawable/logo_blue_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<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> <!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity android:name=".InfoActivity"
android:screenOrientation="portrait" />
<activity android:name=".SendFactActivity"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait" />
<receiver android:name=".FactWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/fact_widget_info" />
</receiver>
<activity android:name=".FactWidgetConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<service android:name=".RebootService"/>
<receiver
android:enabled="true"
android:exported="true"
android:name=".StartServiceOnStartup"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
请求:
private class SendFact extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String login_url = "http://" + IP_ADDRESS + "/FactFolder/sendFact.php";
try {
String fact = params[0];
String owner = params[1];
String socialMediaType = params[2];
String socialMediaName = params[3];
URL url = new URL(login_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream outputStream = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("fact", "UTF-8")+"="+ URLEncoder.encode(fact, "UTF-8")+"&"+
URLEncoder.encode("owner", "UTF-8")+"="+ URLEncoder.encode(owner, "UTF-8")+"&"+
URLEncoder.encode("socialmediatype", "UTF-8")+"="+ URLEncoder.encode(socialMediaType, "UTF-8")+"&"+
URLEncoder.encode("socialmedianame", "UTF-8")+"="+ URLEncoder.encode(socialMediaName, "UTF-8");
writer.write(post_data);
writer.flush();
writer.close();
outputStream.close();
InputStream inputStream = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); //iso-8859-1
String result = "";
result += reader.readLine();
reader.close();
inputStream.close();
con.disconnect();
return result;
} catch (MalformedURLException e) {
return e.getMessage();
} catch (IOException e) {
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
sendFact_finish(result);
}
@Override
protected void onPreExecute() {}
}