我是Android新手,我正在尝试使用Android HTTPUrlConnection将值发布到localhost中的网站。我需要使用GET方法。以下是我的代码
int azi = round(position[0]*100);
try {
URL url = new URL("http://192.168.1.102/arm/arm.php?a="+String.valueOf(azi));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Log.i("Post", "stage 1");
InputStream response = urlConnection.getInputStream();
Log.i("Post", "stage 2");
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
在日志中,我明白了,
02-18 09:53:26.302 29331-29331/com.learnandroid.arm D/Post: stage 1
02-18 10:11:46.804 13855-13855/com.learnandroid.arm W/System.err: java.net.SocketException: Permission denied
02-18 10:11:46.814 13855-13855/com.learnandroid.arm W/System.err: at java.net.Socket.createImpl(Socket.java:473)
02-18 10:11:46.814 13855-13855/com.learnandroid.arm W/System.err: at java.net.Socket.getImpl(Socket.java:536)
02-18 10:11:46.814 13855-13855/com.learnandroid.arm W/System.err: at java.net.Socket.setSoTimeout(Socket.java:1127)
02-18 10:11:46.814 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.Connection.connectSocket(Connection.java:195)
02-18 10:11:46.814 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.Connection.connect(Connection.java:172)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:247)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.learnandroid.arm.MainActivity.writePositionToFile(MainActivity.java:150)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at com.learnandroid.arm.MainActivity.updateValueFields(MainActivity.java:98)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at android.view.View.performClick(View.java:5637)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at android.view.View$PerformClick.run(View.java:22433)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
02-18 10:11:46.815 13855-13855/com.learnandroid.arm W/System.err: at android.os.Looper.loop(Looper.java:154)
02-18 10:11:46.816 13855-13855/com.learnandroid.arm W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6126)
02-18 10:11:46.816 13855-13855/com.learnandroid.arm W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-18 10:11:46.816 13855-13855/com.learnandroid.arm W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
02-18 10:11:46.816 13855-13855/com.learnandroid.arm W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是什么行为?我甚至试过
urlConnection.connect();
结果是一样的!
答案 0 :(得分:0)
看起来您的清单文件中没有INTERNET
权限。
您必须在AndroidManifest.xml
。
<uses-permission android:name="android.permission.INTERNET" />