私はまだ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;
}
ログキャット:
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)