简体   繁体   中英

Unable to POST data WCF from android

I tried for a day still not able to post data to wcf. Here is my code on the server in .net . When i see log from android Logcat, it's always stop at response = httpClient.execute(request); and in the error log nothing capture, the e.getMessage() return null. So i have no idea what is going on.... Any one can spot what's wrong with the below code?

Interface:

<OperationContract()> _
   <WebInvoke(Method:="POST", 
   RequestFormat:=WebMessageFormat.Json,    
   ResponseFormat:=WebMessageFormat.Json, 
   BodyStyle:=WebMessageBodyStyle.Bare, 
   UriTemplate:="SaveEmail")> _
    Function SaveEmail(ByVal emailreceiver As clsEmailRecipe) As String

Code:

    Public Function SaveEmail(emailreceiver As clsEmailRecipe) As String Implements IRestServiceImpl.SaveEmail
        Return "0"
    End Function

Android Client Code:

public static String SendEmail(String url,String Data) { 
     // POST request to <service>/SaveVehicle
    HttpPost request = new HttpPost(url);
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");
    String sText ="";
    // Build JSON string
    JSONStringer email;
    try {
        email = new JSONStringer()
            .object()
                .key("clsEmailRecipe")
                    .object()
                        .key("emailAddress").value("11s22@r.com")
                        .key("campaignId").value("1")
                        .key("recipeId").value("2") 
                    .endObject()
                .endObject();
        StringEntity entity;
        entity = new StringEntity(email.toString());
        request.setEntity(entity);


        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;
        Log.d("SendEmail","1");
        response = httpClient.execute(request); // This Line Cause error
        Log.d("SendEmail","2");
        sText=response.getStatusLine().toString();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        //Log.d("SendEmail",e.getMessage());
         Log.d("SendEmail","Error with null message");
         if (e.getMessage() !=null){
             Log.d("SendEmail",e.getMessage());  
         }

        e.printStackTrace();
    }


      return sText;

} 

Logcat:

03-21 17:36:03.735: D/PostVehicle(23079): 1
03-21 17:36:03.740: D/PostVehicle(23079): Error with null message
03-21 17:36:03.740: W/System.err(23079): android.os.NetworkOnMainThreadException
03-21 17:36:03.740: W/System.err(23079):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
03-21 17:36:03.740: W/System.err(23079):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-21 17:36:03.740: W/System.err(23079):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-21 17:36:03.740: W/System.err(23079):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-21 17:36:03.740: W/System.err(23079):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-21 17:36:03.740: W/System.err(23079):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-21 17:36:03.740: W/System.err(23079):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-21 17:36:03.740: W/System.err(23079):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-21 17:36:03.740: W/System.err(23079):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
03-21 17:36:03.745: W/System.err(23079):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
03-21 17:36:03.745: W/System.err(23079):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-21 17:36:03.745: W/System.err(23079):    at com.omg.rc.general.Utils.PostVehicle(Utils.java:168)
03-21 17:36:03.745: W/System.err(23079):    at com.omg.rc.LoginActivity.onClick(LoginActivity.java:340)
03-21 17:36:03.745: W/System.err(23079):    at android.view.View.performClick(View.java:4223)
03-21 17:36:03.745: W/System.err(23079):    at android.view.View$PerformClick.run(View.java:17275)
 03-21 17:36:03.745: W/System.err(23079):   at android.os.Handler.handleCallback(Handler.java:615)
03-21 17:36:03.745: W/System.err(23079):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 17:36:03.745: W/System.err(23079):    at android.os.Looper.loop(Looper.java:137)
03-21 17:36:03.745: W/System.err(23079):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-21 17:36:03.745: W/System.err(23079):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 17:36:03.745: W/System.err(23079):    at java.lang.reflect.Method.invoke(Method.java:511)
03-21 17:36:03.745: W/System.err(23079):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-21 17:36:03.745: W/System.err(23079):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-21 17:36:03.745: W/System.err(23079):    at dalvik.system.NativeStart.main(Native Method)

Run your SendEmail(String url,String Data) method inside an AsyncTask . Networking operations are not allowed to be run inside your main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM