在Android中使用WCF服务时出错

时间:2021-08-29 23:17:49

I am trying to POST data to my database using WCF service but can't seem to figure what is wrong with my code, I have tried different solutions but all of it seems to fail. I am new to android development and do not know how to debug. Please help me...
Here is my code:

我正在尝试使用WCF服务将数据发布到我的数据库,但似乎无法确定我的代码有什么问题,我尝试了不同的解决方案,但所有这些似乎都失败了。我是Android开发的新手,不知道如何调试。请帮帮我......这是我的代码:

WCF Service:

[ServiceContract]
public interface IAllocationService
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "StrongestWifi",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    void UpdateStrongestWifi(myWifiClass myWifiClass);

}

[DataContract]
public class myWifiClass
{
    [DataMember(Name = "Time")]
    public string Time
    {
        get;
        set;
    }
    [DataMember(Name = "SignalStrength")]
    public string SignalStrength
    {
        get;
        set;
    }
    [DataMember(Name = "SSID")]
    public string SSID
    {
        get;
        set;
    }

Android Code:

public void onGetWebService(View v)
{
    HttpPost request = new HttpPost(SERVICE_URI + "/StrongestWifi");
    request.setHeader("Accept", "application/json");            
    request.setHeader("Content-type", "application/json");
    JSONStringer getWifiInfo;
    try {
        getWifiInfo = new JSONStringer()
            .object()
                .key("myWifiClass")
                    .object()
                        .key("Time").value("TIME")                                  
                        .key("SignalStrength").value("Strength")
                        .key("SSID").value("ID")
                    .endObject()
                .endObject();

    StringEntity entity = new StringEntity(getWifiInfo.toString());

    request.setEntity(entity);

    // Send request to WCF service
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);
    Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Log Cat:

06-01 15:46:06.229: W/System.err(27711): android.os.NetworkOnMainThreadException 06-01 15:46:06.237: W/System.err(27711): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 06-01 15:46:06.237: W/System.err(27711): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 06-01 15:46:06.237: W/System.err(27711): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 06-01 15:46:06.237: W/System.err(27711): at libcore.io.IoBridge.connect(IoBridge.java:112) 06-01 15:46:06.245: W/System.err(27711): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 06-01 15:46:06.245: W/System.err(27711): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 06-01 15:46:06.245: W/System.err(27711): at java.net.Socket.connect(Socket.java:842) 06-01 15:46:06.245: W/System.err(27711): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 06-01 15:46:06.245: W/System.err(27711): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 06-01 15:46:06.253: W/System.err(27711): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 06-01 15:46:06.253: W/System.err(27711): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 06-01 15:46:06.253: W/System.err(27711): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 06-01 15:46:06.253: W/System.err(27711): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 06-01 15:46:06.253: W/System.err(27711): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 06-01 15:46:06.253: W/System.err(27711): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 06-01 15:46:06.253: W/System.err(27711): at com.example.pcsprojectnetworkcodes.MainActivity.onGetWebService(MainActivity.java:191) 06-01 15:46:06.253: W/System.err(27711): at java.lang.reflect.Method.invokeNative(Native Method) 06-01 15:46:06.261: W/System.err(27711): at java.lang.reflect.Method.invoke(Method.java:511) 06-01 15:46:06.261: W/System.err(27711): at android.view.View$1.onClick(View.java:3594) 06-01 15:46:06.261: W/System.err(27711): at android.view.View.performClick(View.java:4204) 06-01 15:46:06.261: W/System.err(27711): at android.view.View$PerformClick.run(View.java:17360) 06-01 15:46:06.261: W/System.err(27711): at android.os.Handler.handleCallback(Handler.java:725) 06-01 15:46:06.261: W/System.err(27711): at android.os.Handler.dispatchMessage(Handler.java:92) 06-01 15:46:06.269: W/System.err(27711): at android.os.Looper.loop(Looper.java:137) 06-01 15:46:06.269: W/System.err(27711): at android.app.ActivityThread.main(ActivityThread.java:5237) 06-01 15:46:06.276: W/System.err(27711): at java.lang.reflect.Method.invokeNative(Native Method) 06-01 15:46:06.276: W/System.err(27711): at java.lang.reflect.Method.invoke(Method.java:511) 06-01 15:46:06.276: W/System.err(27711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) 06-01 15:46:06.276: W/System.err(27711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 06-01 15:46:06.284: W/System.err(27711): at dalvik.system.NativeStart.main(Native Method)

06-01 15:46:06.229:W / System.err(27711):android.os.NetworkOnMainThreadException 06-01 15:46:06.237:W / System.err(27711):在android.os.StrictMode $ AndroidBlockGuardPolicy。 onNetwork(StrictMode.java:1117)06-01 15:46:06.237:W / System.err(27711):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)06-01 15:46:06.237: W / System.err(27711):at libcore.io.IoBridge.connectErrno(IoBridge.java:127)06-01 15:46:06.237:W / System.err(27711):at libcore.io.IoBridge.connect (IoBridge.java:112)06-01 15:46:06.245:W / System.err(27711):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)06-01 15:46:06.245:W /System.err(27711):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)06-01 15:46:06.245:W / System.err(27711):at java.net.Socket.connect( Socket.java:842)06-01 15:46:06.245:W / System.err(27711):at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)06-01 15: 46:06.245:W / System.err(27711):at org.apache.http.impl.conn.DefaultClientConn ectionOperator.openConnection(DefaultClientConnectionOperator.java:144)06-01 15:46:06.253:W / System.err(27711):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)06 -01 15:46:06.253:W / System.err(27711):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)06-01 15:46:06.253:W / System .err(27711):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)06-01 15:46:06.253:W / System.err(27711):at org.apache。 http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)06-01 15:46:06.253:W / System.err(27711):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient) .java:487)06-01 15:46:06.253:W / System.err(27711):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)06-01 15:46 :06.253:W / System.err(27711):at com.example.pcsprojectnetworkcodes.MainActivity.onGetWebService(MainActivity.java:191)06-01 15:46:06.253: W / System.err(27711):at java.lang.reflect.Method.invokeNative(Native Method)06-01 15:46:06.261:W / System.err(27711):at java.lang.reflect.Method。 invoke(Method.java:511)06-01 15:46:06.261:W / System.err(27711):at android.view.View $ 1.onClick(View.java:3594)06-01 15:46:06.261 :W / System.err(27711):在android.view.View.performClick(View.java:4204)06-01 15:46:06.261:W / System.err(27711):at android.view.View $ PerformClick.run(View.java:17360)06-01 15:46:06.261:W / System.err(27711):at android.os.Handler.handleCallback(Handler.java:725)06-01 15:46: 06.261:W / System.err(27711):在android.os.Handler.dispatchMessage(Handler.java:92)06-01 15:46:06.269:W / System.err(27711):在android.os.Looper .loop(Looper.java:137)06-01 15:46:06.269:W / System.err(27711):在android.app.ActivityThread.main(ActivityThread.java:5237)06-01 15:46:06.276 :W / System.err(27711):at java.lang.reflect.Method.invokeNative(Native Method)06-01 15:46:06.276:W / System.err(27711):at java.lang.reflect.Method .invoke(MET hod.java:511)06-01 15:46:06.276:W / System.err(27711):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:799)06-01 15: 46:06.276:W / System.err(27711):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)06-01 15:46:06.284:W / System.err(27711): at dalvik.system.NativeStart.main(Native Method)

1 个解决方案

#1


2  

Dont make Network call on main thread as it may crash application.try to call this httpClient.execute(request) in asyntask or using runnable thread.

不要在主线程上进行网络调用,因为它可能会使application.try在asyntask或使用runnable线程中调用此httpClient.execute(请求)。

#1


2  

Dont make Network call on main thread as it may crash application.try to call this httpClient.execute(request) in asyntask or using runnable thread.

不要在主线程上进行网络调用,因为它可能会使application.try在asyntask或使用runnable线程中调用此httpClient.execute(请求)。