Java HttpClient错误
我尝试使用http客户端通过web服务器上的php脚本执行一些简单的snmp操作。因此php脚本侦听以获取参数。这是我的密码:Java HttpClient错误,java,android,apache-httpclient-4.x,Java,Android,Apache Httpclient 4.x,我尝试使用http客户端通过web服务器上的php脚本执行一些简单的snmp操作。因此php脚本侦听以获取参数。这是我的密码: EditText host = (EditText)findViewById(R.id.editText); EditText port = (EditText)findViewById(R.id.editText2); EditText community = (EditText)findViewById(R.id.edi
EditText host = (EditText)findViewById(R.id.editText);
EditText port = (EditText)findViewById(R.id.editText2);
EditText community = (EditText)findViewById(R.id.editText3);
String url = "http://192.168.0.102/za/getstatus.php?host=" + host.getText().toString() + "&port=" + port.getText().toString() + "&com=" + community.getText().toString() + "&oid=" + 2 + "&port1=" + 2;
// here is my url = http://192.168.0.102/za/getstatus.php?
// host=192.168.0.101&port=10100&com=private&oid=2&port1=2
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpclient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
发送Http post时应该得到的响应是整数:0或整数:1,因此我应该检查该结果
尝试发送http post时的错误日志:
12-29 06:48:21.979 1545-1545/com.example.test.test1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.test.test1, PID: 1545
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:843)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.zara.sunsannici.MainActivity.selfDestruct(MainActivity.java:51)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
12-29 06:48:24.919 1545-1545/com.example.test.test1 I/Process﹕ Sending signal. PID: 1545 SIG: 9
也许我还可以用其他的方法。例如,在C#中,我使用更易于使用和获取返回文本的WebClient
谢谢您的帮助。您无法在UI线程中创建它 下面是一个使用AsyncTask的示例
EditText host = (EditText)findViewById(R.id.editText);
EditText port = (EditText)findViewById(R.id.editText2);
EditText community = (EditText)findViewById(R.id.editText3);
String url = "http://192.168.0.102/za/getstatus.php?host=" + host.getText().toString() + "&port=" + port.getText().toString() + "&com=" + community.getText().toString() + "&oid=" + 2 + "&port1=" + 2;
new Task(url).execute();
}
class Task extends AsyncTask<Void, Void, HttpResponse>{
private String url;
Task(String url) {
this.url = url;
}
@Override
protected HttpResponse doInBackground(Void... voids) {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpPost httpPost = new HttpPost(url);
try {
return httpclient.execute(httpPost);
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
return null;
}
@Override
protected void onPostExecute(HttpResponse httpResponse) {
super.onPostExecute(httpResponse);
if(httpResponse != null){
Log.d("Http Response:", response.toString());
}
}
}
EditText主机=(EditText)findViewById(R.id.EditText);
EditText端口=(EditText)findViewById(R.id.editText2);
EditText社区=(EditText)findViewById(R.id.editText3);
字符串url=”http://192.168.0.102/za/getstatus.php?host=“+host.getText().toString()+”&port=“+port.getText().toString()+”&com=“+community.getText().toString()+”&oid=“+2+”&port1=“+2;
新任务(url).execute();
}
类任务扩展了异步任务{
私有字符串url;
任务(字符串url){
this.url=url;
}
@凌驾
受保护的HttpResponse doInBackground(无效…无效){
HttpClient HttpClient=新的DefaultHttpClient();
//准备一个请求对象
HttpPost HttpPost=新的HttpPost(url);
试一试{
返回httpclient.execute(httpPost);
}捕获(客户端协议例外e){
//将异常写入日志
e、 printStackTrace();
}捕获(IOE异常){
//将异常写入日志
e、 printStackTrace();
返回null;
}
@凌驾
受保护的void onPostExecute(HttpResponse HttpResponse){
super.onPostExecute(httpResponse);
if(httpResponse!=null){
Log.d(“Http响应:,Response.toString());
}
}
}
如错误所示,您不能在主活动上使用http请求(POST/get)。请创建另一个活动来执行此操作或创建一个异步任务。