我试了一天仍然无法将数据发布到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)
答案 0 :(得分:0)
在SendEmail(String url,String Data)
内运行AsyncTask
方法。不允许在主线程内运行网络操作。