HttpClient 错误 [英] HttpClient error

查看:37
本文介绍了HttpClient 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http 客户端通过 web 服务器上的 php 脚本执行一些简单的 snmp 操作.所以php脚本监听GET参数.这是我的代码:

I'm tring to use http client to perform some simple snmp operations via a php script on a web server. So the php script listen to GET parameters. Here is my code:

 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 帖子时我应该得到的响应是 INTEGER: 0 或 INTEGER: 1 所以我应该检查那个结果.

The response that I should get when I send the Http post is INTEGER: 0 or INTEGER: 1 so I should check that result.

当我尝试发送 http 帖子时的错误日志:

Error Log when I try to send a 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,它更易于使用和获取返回的文本.

Maybe there is some other methods that I can use. For example in C# I use WebClient that is more easy to use and to get the returned text.

感谢您的帮助.

推荐答案

你不能在UI线程中制作

you can not make it in the UI thread

这是一个使用 AsyncTask 的例子

Here is an example using 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());
            }
        }
    }

这篇关于HttpClient 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆