0

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

WCF 服务:

[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;
    }

安卓代码:

public void onGetWebService(View v)
{
    new sendPostData().execute("");
}

private class sendPostData extends AsyncTask<String, Void, String>
{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        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());
        textDisplay.setText("Success");
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

原木猫:

>06-01 20:36:44.071: E/SpannableStringBuilder(1071): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-01 20:36:44.071: E/SpannableStringBuilder(1071): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-01 20:36:44.087: E/SpannableStringBuilder(1071): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-01 20:36:44.087: E/SpannableStringBuilder(1071): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-01 20:36:45.157: D/WebInvoke(1071): Saving : 400
>06-01 20:36:45.157: W/System.err(1071): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
>06-01 20:36:45.157: W/System.err(1071):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
>06-01 20:36:45.157: W/System.err(1071):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
>06-01 20:36:45.157: W/System.err(1071):    at android.view.View.requestLayout(View.java:15478)
>06-01 20:36:45.157: W/System.err(1071):    at android.view.View.requestLayout(View.java:15478)
>06-01 20:36:45.157: W/System.err(1071):    at android.view.View.requestLayout(View.java:15478)
>06-01 20:36:45.157: W/System.err(1071):    at android.view.View.requestLayout(View.java:15478)
>06-01 20:36:45.165: W/System.err(1071):    at android.view.View.requestLayout(View.java:15478)
>06-01 20:36:45.165: W/System.err(1071):    at android.widget.TextView.checkForRelayout(TextView.java:6457)
>06-01 20:36:45.165: W/System.err(1071):    at android.widget.TextView.setText(TextView.java:3701)
>06-01 20:36:45.165: W/System.err(1071):    at android.widget.TextView.setText(TextView.java:3559)
>06-01 20:36:45.165: W/System.err(1071):    at android.widget.TextView.setText(TextView.java:3534)
>06-01 20:36:45.165: W/System.err(1071):    at com.example.pcsprojectnetworkcodes.MainActivity$sendPostData.doInBackground(MainActivity.java:202)
>06-01 20:36:45.165: W/System.err(1071):    at com.example.pcsprojectnetworkcodes.MainActivity$sendPostData.doInBackground(MainActivity.java:1)
>06-01 20:36:45.165: W/System.err(1071):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
>06-01 20:36:45.165: W/System.err(1071):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
>06-01 20:36:45.165: W/System.err(1071):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
>06-01 20:36:45.165: W/System.err(1071):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
>06-01 20:36:45.165: W/System.err(1071):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
>06-01 20:36:45.173: W/System.err(1071):    at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

1

移动线:

textDisplay.setText("Success");

从 doBackground() 方法到 PostExecute() 方法。

于 2013-06-01T12:41:14.657 回答