AsyncTask中的错误无法发送POST数据

时间:2013-06-01 12:38:52

标签: c# java android .net android-asynctask

我正在尝试使用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;
    }

Android代码:

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

}

Log Cat:

>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)

1 个答案:

答案 0 :(得分:1)

移动线:

textDisplay.setText("Success");

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