【问题标题】:Unable to POST data WCF from android无法从 Android POST 数据 WCF
【发布时间】:2013-03-21 09:23:59
【问题描述】:

我尝试了一天仍然无法将数据发布到 wcf。这是我在.net服务器上的代码 .当我看到来自 android Logcat 的日志时,它总是停在 response = httpClient.execute(request);并且在错误日志中没有捕获任何内容,e.getMessage() 返回 null。所以我不知道发生了什么......任何人都可以发现下面的代码有什么问题?

界面:

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

代码:

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

Android 客户端代码:

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)

【问题讨论】:

  • 你能发布你的 logcat 吗?
  • 在帖子中包含 Log cat,如果您怀疑是因为 Androidmanifest.xml 中的权限,我已经添加了这个

标签: android json wcf post


【解决方案1】:

AsyncTask 中运行您的SendEmail(String url,String Data) 方法。网络操作不允许在你的主线程中运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多