RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

How to post data from my android app to server?

i want to send data from android application to server (.NET) but it doesn't work this is my code :

DefaultHttpClient hc=new DefaultHttpClient();  
ResponseHandler <String> res=new BasicResponseHandler();  
HttpPost postMethod=new HttpPost(myURl);  

postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
postMethod.getParams().setBooleanParameter( "http.protocol.expect-continue", false );
postMethod.setHeader( "Content-Type", "application/json" );

JSONObject json = new JSONObject();

json.put("TOKEN", channel_token).toString();
json.put("APPLICATIONDATASOURCEID", data_src_id).toString();
json.put("NEWSTITLE", Title_edittext.getText().toString().trim()).toString();
json.put("NEWSDETAILS", Details_edittext.getText().toString()).toString();      
json.put("ALERTSTARTSAT" , "12/03/2012/05/12");
json.put("ALERTENDSAT", "13/03/2012/06/12");
json.put("SENDPUSHNOTIFICATION", true);
json.put("EXPIREIMMEDIATELY", true);

Log.i("jason Object", json.toString());

StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
postMethod.setEntity(se);      

HttpResponse response = hc.execute(postMethod); 
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String _response = convertStreamToString(is);
System.out.println("res  " + _response);

getting this as response from server "Expectaions Failed" . please tell me where is the problem.

Upvotes: 0

Views: 692

Answers (3)

Riddhish.Chaudhari
Riddhish.Chaudhari

Reputation: 853

You have to use a WSDL web service to post data on a .NET web server, because the .NET framework does not provide WSDL connectivity and it does not support direct HttpPost(myURL) like Java.

I hope this article will help you.

Upvotes: 0

Rajesh Kolappakam
Rajesh Kolappakam

Reputation: 2125

If you are looking for a tutorial, the Android SDK comes with a sample app called Wikitionary. It is a good example to understand HTTP Get with JSON.

Upvotes: 0

waqaslam
waqaslam

Reputation: 68187

set your Json request as below:

final int TIMEOUT_MILLISEC = 10000;  // = 10 seconds

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);

HttpPost request = new HttpPost(myURl);

//set post request type
request.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");

//request result type
request.setHeader("Accept", "application/json; charset=utf-8");

JSONObject json = new JSONObject();
.
.
.
.
.
//and so on with rest of the code

Upvotes: 1

Related Questions