我正在尝试将数据从android发布到wcf但是它的发送消息不正常 ,我能够从服务中检索数据....但无法发布... 这是代码:
HttpPost request = new HttpPost(http://xxx.xxx.x.xx/JsonWcfService/GetEmployees.svc/json/addemp); request.setHeader(" Accept"," application / json"); request.setHeader(" Content-type"," application / json"); String Stext ="&#34 ;; String not = new String("");
try {
// Build JSON string
JSONStringer vehicle = new JSONStringer()
.object()
.key("rData")
.object()
.key("details").value("241|b@h.us|why")
.endObject()
.endObject();
StringEntity entity = new StringEntity(vehicle.toString());
Toast.makeText(MainActivity.this, vehicle.toString() + "\n", Toast.LENGTH_LONG).show();
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
// Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
Toast.makeText(MainActivity.this, response.getStatusLine().getStatusCode() + "\n", Toast.LENGTH_LONG).show();
Stext = response.getStatusLine().toString();
} catch (Exception e) {
not = "NOT ";
e.printStackTrace();
}
Toast.makeText(MainActivity.this, not + " OK ! " + "\n" + Stext, Toast.LENGTH_LONG).show();
以下是服务:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/addemp")]
bool AddJsonUser(RequestData rData);
public bool AddUser(RequestData rData) {
bool returnBool = false;
var data = rData.details.Split('|');
var response = new EmployeeData {
empid = data[0],
empname = data[1],
emppass = data[2],
};
OracleConnection dbConn = new OracleConnection("Data Source=xxx;User Id=xxx;Password=xxxx");
//string sqlStr = "INSERT INTO signup_table('user_id','user_firstname','user_pass') values('" + data[0] + "', '" + data[1] + "', '" + data[2] +"');";
string p_name = "signup_proc";
OracleCommand cmnd = new OracleCommand(p_name, dbConn);
cmnd.CommandType = CommandType.StoredProcedure;
cmnd.Parameters.Add("u_id", OracleDbType.Int32).Value = Convert.ToInt32(data[0]);
cmnd.Parameters.Add("u_name", OracleDbType.Varchar2).Value = data[1];
cmnd.Parameters.Add("u_pass", OracleDbType.Varchar2).Value = data[2];
try {
dbConn.Open();
if (cmnd.ExecuteNonQuery() != 0) {
returnBool = true;
}
dbConn.Close();
} catch {
returnBool = false;
}
return returnBool;
}
public bool AddJsonUser(RequestData rData) {
return AddUser(rData);
}
请帮助,我无法弄清楚发生了什么......
这里是logcat .....
04-27 04:23:44.930: D/LightsService(389): Excessive delay setting light: 54ms
04-27 04:23:45.260: D/LightsService(389): Excessive delay setting light: 53ms
04-27 04:23:45.390: D/LightsService(389): Excessive delay setting light: 121ms
04-27 04:23:45.420: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.420: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.420: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.420: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.450: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.460: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.460: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.460: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.460: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.490: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
04-27 04:23:45.510: E/SoundPool(389): error loading /system/media/audio/ui/KeypressStandard.ogg
04-27 04:23:45.510: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
04-27 04:23:45.510: E/SoundPool(389): error loading /system/media/audio/ui/KeypressSpacebar.ogg
04-27 04:23:45.520: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
04-27 04:23:45.520: E/SoundPool(389): error loading /system/media/audio/ui/KeypressDelete.ogg
04-27 04:23:45.520: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
04-27 04:23:45.520: E/SoundPool(389): error loading /system/media/audio/ui/KeypressReturn.ogg
04-27 04:23:45.530: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
04-27 04:23:45.530: E/SoundPool(389): error loading /system/media/audio/ui/KeypressInvalid.ogg
04-27 04:23:45.530: W/AudioService(389): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
04-27 04:23:45.530: W/AudioService(389): onLoadSoundEffects(), Error -1 while loading samples
04-27 04:23:45.580: W/System.err(1171): android.os.NetworkOnMainThreadException
04-27 04:23:45.590: W/System.err(1171): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
04-27 04:23:45.610: W/System.err(1171): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-27 04:23:45.620: W/System.err(1171): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-27 04:23:45.630: W/System.err(1171): at libcore.io.IoBridge.connect(IoBridge.java:112)
04-27 04:23:45.630: W/System.err(1171): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-27 04:23:45.630: W/System.err(1171): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-27 04:23:45.630: W/System.err(1171): at java.net.Socket.connect(Socket.java:843)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-27 04:23:45.640: W/System.err(1171): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-27 04:23:45.640: W/System.err(1171): at com.rhs.postdata.MainActivity$1.onClick(MainActivity.java:63)
04-27 04:23:45.640: W/System.err(1171): at android.view.View.performClick(View.java:4438)
04-27 04:23:45.660: W/System.err(1171): at android.view.View$PerformClick.run(View.java:18422)
04-27 04:23:45.660: W/System.err(1171): at android.os.Handler.handleCallback(Handler.java:733)
04-27 04:23:45.660: W/System.err(1171): at android.os.Handler.dispatchMessage(Handler.java:95)
04-27 04:23:45.660: W/System.err(1171): at android.os.Looper.loop(Looper.java:136)
04-27 04:23:45.680: W/System.err(1171): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-27 04:23:45.680: W/System.err(1171): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 04:23:45.680: W/System.err(1171): at java.lang.reflect.Method.invoke(Method.java:515)
04-27 04:23:45.700: W/System.err(1171): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-27 04:23:45.710: W/System.err(1171): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-27 04:23:45.710: W/System.err(1171): at dalvik.system.NativeStart.main(Native Method)
04-27 04:23:46.960: D/dalvikvm(525): GC_FOR_ALLOC freed 594K, 17% free 3327K/4000K, paused 161ms, total 165ms
04-27 04:24:46.770: D/LightsService(389): Excessive delay setting light: 65ms
04-27 04:25:18.790: D/ConnectivityService(389): Sampling interval elapsed, updating statistics ..
04-27 04:25:18.900: D/ConnectivityService(389): Done.
04-27 04:25:18.900: D/ConnectivityService(389): Setting timer for 720seconds
答案 0 :(得分:1)
logcat告诉您有NetworkOnMainThreadException
。将您的网络/ http代码放在AsyncTask或线程中。从代码中删除Toast()。