httpClient不发送数据?

时间:2013-09-28 05:22:09

标签: android client httpclient

我写了以下代码。

public void postSMS(String type,String phone,String body) 
    {
        //----------------------postSMS-------------------------------
                // Create a new HttpClient and Post Header
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://192.168.15.105:6543/data_in");

                    try {
                        // Add your data

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                        nameValuePairs.add(new BasicNameValuePair("type", type));
                        nameValuePairs.add(new BasicNameValuePair("body", body));
                        nameValuePairs.add(new BasicNameValuePair("toRfrom", phone));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        // Execute HTTP Post Request
                        //HttpResponse response = httpclient.execute(httppost);
                        ResponseHandler<String> responseHandler=new BasicResponseHandler(); 
                        String responseBody = httpclient.execute(httppost, responseHandler); 
                        JSONObject response=new JSONObject(responseBody); 
                    } 
                    catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    }

错误是什么以及如何解决?由于我的服务器工作正常,但它是不发送数据的客户端。(我在服务器上使用python的PYCK框架。)

更新 是的,我在清单中添加了INTERNET权限......

以下是错误日志:

10-01 15:52:32.133: E/AndroidRuntime(7757): FATAL EXCEPTION: main
10-01 15:52:32.133: E/AndroidRuntime(7757): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.try1/com.example.try1.Connection}: android.os.NetworkOnMainThreadException
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2191)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2218)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.ActivityThread.access$600(ActivityThread.java:149)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.os.Looper.loop(Looper.java:153)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.ActivityThread.main(ActivityThread.java:5026)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at java.lang.reflect.Method.invoke(Method.java:511)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at dalvik.system.NativeStart.main(Native Method)
10-01 15:52:32.133: E/AndroidRuntime(7757): Caused by: android.os.NetworkOnMainThreadException
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at java.net.Socket.connect(Socket.java:857)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at com.example.try1.Connection.connection(Connection.java:62)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at com.example.try1.Connection.onCreate(Connection.java:34)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.Activity.performCreate(Activity.java:5020)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-01 15:52:32.133: E/AndroidRuntime(7757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2155)
10-01 15:52:32.133: E/AndroidRuntime(7757):     ... 11 more
10-01 15:52:32.718: I/Process(7757): Sending signal. PID: 7757 SIG: 9

2 个答案:

答案 0 :(得分:0)

1-如何通过端口6543发送数据。

解决方案很简单。将您的地址更改为以下

HttpPost httppost = new HttpPost("http://192.168.15.105:6543/data_in");

2-您的代码有任何错误吗?

我测试了你的代码,没有问题。所以,如果你的服务器发送有效的json字符串,那就应该没问题。

答案 1 :(得分:0)

<强> android.os.NetworkOnMainThreadException 这就是问题所在。

您需要在后台线程上运行使用Internet的所有内容。

你可以使用AsyncTask,runnable或者只是创建一个新线程,最好的方法肯定是第一个。