此问题可能已经提出过,但针对不同的问题。 以下cURL命令可以正常使用shell命令:
curl -u MyuserName:MyPassword http://192.168.0.198:8067/startvideo?force=1&tag=20141010-121420
我尝试在Java / Android中实现它:
private void startRecord() {
HttpClient httpClient = new DefaultHttpClient();
// replace with your url
HttpPost httpPost = new HttpPost("http://192.168.0.198:8067/startvideo?force=1&tag=20141010-121420");
//Post Data
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("username", "MyuserName"));
nameValuePair.add(new BasicNameValuePair("password", "MyPassword"));
//Encoding POST data
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// log exception
e.printStackTrace();
}
//making POST request.
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
}
但该应用程序崩溃并出现以下错误:
10-12 12:15:41.245: E/AndroidRuntime(25877): FATAL EXCEPTION: main
10-12 12:15:41.245: E/AndroidRuntime(25877): java.lang.RuntimeException: Unable to create service ioio.examples.hello_service.TestService: android.os.NetworkOnMainThreadException
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2424)
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.app.ActivityThread.access$1700(ActivityThread.java:140)
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.os.Looper.loop(Looper.java:137)
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.app.ActivityThread.main(ActivityThread.java:4921)
10-12 12:15:41.245: E/AndroidRuntime(25877): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 12:15:41.245: E/AndroidRuntime(25877): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 12:15:41.245: E/AndroidRuntime(25877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
10-12 12:15:41.245: E/AndroidRuntime(25877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
10-12 12:15:41.245: E/AndroidRuntime(25877): at dalvik.system.NativeStart.main(Native Method)
10-12 12:15:41.245: E/AndroidRuntime(25877): Caused by: android.os.NetworkOnMainThreadException
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
10-12 12:15:41.245: E/AndroidRuntime(25877): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-12 12:15:41.245: E/AndroidRuntime(25877): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-12 12:15:41.245: E/AndroidRuntime(25877): at libcore.io.IoBridge.connect(IoBridge.java:112)
10-12 12:15:41.245: E/AndroidRuntime(25877): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-12 12:15:41.245: E/AndroidRuntime(25877): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-12 12:15:41.245: E/AndroidRuntime(25877): at java.net.Socket.connect(Socket.java:842)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
10-12 12:15:41.245: E/AndroidRuntime(25877): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-12 12:15:41.245: E/AndroidRuntime(25877): at ioio.examples.hello_service.TestService.start_record2(TestService.java:77)
10-12 12:15:41.245: E/AndroidRuntime(25877): at ioio.examples.hello_service.TestService.onCreate(TestService.java:34)
10-12 12:15:41.245: E/AndroidRuntime(25877): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2414)
10-12 12:15:41.245: E/AndroidRuntime(25877): ... 10 more