httpClient not sending data?

Ifrah

I have written the following code.

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();
                    }
    }

What's the error and how to resolve it? Since my server is working fine but it's the client that's not sending data.(i am using python's PYCK framework on server.)

UPDATE: yes I have added INTERNET permission in the manifest ...

Following is the error log:

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
Sulfkain

android.os.NetworkOnMainThreadException That's the problem.

You need to run everything that use Internet on a background Thread.

You can use AsyncTask, runnable or just create a new Thread, the best way will be surely the first one.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related