0

I am trying to post data from android to wcf but its giving message not ok ,i am able to retrieve data from the service....but can't post it... here is the code:

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 = ""; String not = new String(" ");

try {
    // Build JSON string
    JSONStringer vehicle = new JSONStringer()
        .object()
        .key("rData")
        .object()
        .key("details").value("241|[email protected]|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();

and below is the service:

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

Please help, I can't figure out what is happening....

here is the 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
7
  • ..but can't post it.. i can't figure out what is happering. Well it starts with you! What does not work exactly? Which code is executed and which not? Errors? Exceptions? Did you look in the logcat? It will tell you a lot. Commented Apr 27, 2015 at 7:44
  • i tried to figure out this way....int var = response.getStatusLine().getStatusCode(); it gives me 400. Commented Apr 27, 2015 at 7:54
  • And? What did you expect/see/conclude? What does 400 mean? Where is that line? I dont see it. Commented Apr 27, 2015 at 7:56
  • i expect code http response code 200 but its giving me http response code 400 which means bad request.... Commented Apr 27, 2015 at 8:01
  • Post the logcat please. Commented Apr 27, 2015 at 8:03

1 Answer 1

1

The logcat tells you that you have a NetworkOnMainThreadException. Put your network/http code in an AsyncTask or thread. Remove the Toast()'s from your code.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank's for your reply .....i'll put it in asynctask....but according to momo's comment i have made some request through my browser rest plugin ...but it gives me bad request status and a message .....which is "The server encountered an error processing the request. See server logs for more details."....what should i do in that case.......
In that case you should look in the server logs ;-). What kind of rest plugin in which browser did you use? But put your code in an AsyncTask first and see what kind of errors/reply you get.
Just set a few breakpoints in your wcf-webservice and go through the code step by step. Maybe there is some NullPointerException. In addition to the response above, also consider to use the modern HttpUrlConnection class instead of the old DefaultHttpClient or to use a network library like Retrofit.
Thank's to all.....for your kind response .....i have finally solved it...the main problem is in my .dll file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.