Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I had tried like this...please let me know if any errors are there


JavaScript
package com.sample;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener 
{
	public XmlPullParserFactory xmlFactoryObject;
	ProgressDialog pd=null;
	EditText user=null,pass=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sample2);
		Button print=(Button) findViewById(R.id.button2);
		Button submit=(Button) findViewById(R.id.button1);
		user=(EditText) findViewById(R.id.editText1);
		pass=(EditText) findViewById(R.id.editText2);
		submit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) 
	{
		LoginDetails details=new LoginDetails();
		details.execute();

	}
	class LoginDetails extends AsyncTask<string,>
	{
		@Override
		public void onPreExecute()
		{
			pd=new ProgressDialog(MainActivity.this);
			pd.setMessage("Please wait....");
			pd.setCancelable(false);
			pd.show();
		}
		@Override
		protected String doInBackground(String... params)
		{
			String username=user.getText().toString();
			String password=pass.getText().toString();
			String inputxele="LoginData UserName='"+username+"' UserPassword='"+password+"'";
			String sp="USP_AreaMst";
			String tt="CreateLoginData";
			String url="http://10.0.2.2/JsonWcfService/GetEmployees.svc/GetAllReportsMethod?";
			String parameters="spName="+URLEncoder.encode(sp)+"&inputxele="+URLEncoder.encode(inputxele)+"&transType="+URLEncoder.encode(tt);
			String ur= url+parameters;
			InputStream instream =null;
			String resultdata=null;
			try
			{
				ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
				nameValuePairs.add(new BasicNameValuePair("spName", sp));
				nameValuePairs.add(new BasicNameValuePair("inputxele", inputxele));
				nameValuePairs.add(new BasicNameValuePair("transType", tt));
				HttpParams httpParameters = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
				HttpConnectionParams.setSoTimeout(httpParameters, 20000);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost post=null;
				try
				{	
					post=new HttpPost(url);
					post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					HttpResponse response = httpclient.execute(post);
					StatusLine statusLine = response.getStatusLine();
					if(statusLine.getStatusCode() == HttpStatus.SC_OK)
					{
						HttpEntity entity = response.getEntity();
						Toast.makeText(MainActivity.this, "Data Inserted sucessfully", Toast.LENGTH_LONG).show();
						instream.close();
					}
					else
					{
						Toast.makeText(MainActivity.this, "Incorrect data", Toast.LENGTH_LONG).show();
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			return resultdata;
		}

		@Override
		protected void onPostExecute(String result)
		{
			super.onPostExecute(result);
			if(pd!=null)
			{
				pd.dismiss();

			}
		}

		//	@Override
		//	public void onClick(View v) 
		//	{	
		//		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
		//		    //handle case of no SDCARD present
		//		} 
		//		else
		//		{
		//		    String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
		//		    //create folder
		//		    File folder = new File(dir); //folder name
		//		    folder.mkdirs();
		//
		//		    //create file
		//		    File file = new File(dir, "raghu.txt");
		//		}
		//	}  
	}
}
Posted
Updated 20-Feb-15 21:05pm
v3
Comments
Wendelius 21-Feb-15 3:06am    
That's just a code dump. Do yo have any specific problems or questions you can point out?
Raghu Golla 21-Feb-15 3:09am    
While using this code i am getting method not allowed and bad request type errors
Richard MacCutchan 21-Feb-15 4:22am    
Well unless you explain where, and provide the exact details, it is unlikely that anyone can help you.
Raghu Golla 21-Feb-15 6:23am    
[WebInvoke(Method="GET",
UriTemplate = "GetAllReportsMethod?spName={spName}&inputxele={inputxele}&transType={transType}",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Xml)]
//method
XElement GetAllReportsMethod(string spName, string inputxele, string transType);
[OperationContract]


I had written my service side code like above,when i invoking the wcf service from my android application from this
post=new HttpPost(url); line it was throwing some errors messages like bad request and method not allowed
Richard MacCutchan 21-Feb-15 6:43am    
some errors messages like bad request and method not allowed
Please re-read my previous suggestion, edit your question, and provide proper clear details about each error: where it occurs in your code, and the exact wording of the message. Please do not expect us to guess what happens, where it happens, or what messages you see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900