I have to request on server in form of an xml and get the response. Currently I am getting the xml not correct error. Don't know where it is wrong or my way is not correct. Below is my Xml and code I am trying.
XML:
<txn><ssl_merchant_id>893</ssl_merchant_id>
<ssl_user_id>page</ssl_user_id><ssl_pin>3472</ssl_pin>
<ssl_test_mode>false</ssl_test_mode><ssl_transaction_type>ccsale
</ssl_transaction_type><ssl_card_number>1234567890123456
</ssl_card_number><ssl_exp_date>1617</ssl_exp_date><ssl_amount>
</ssl_amount></txn>
Code I have tried:
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://demo.myvirtualmerchant.com/VirtualMerchantDemo/processxml.do");
try {
StringEntity se = new StringEntity( "<txn><ssl_merchant_id>893</ssl_merchant_id>"+
"<ssl_user_id>page</ssl_user_id><ssl_pin>3472</ssl_pin>"+
"<ssl_test_mode>false</ssl_test_mode><ssl_transaction_type>ccsale"+
"</ssl_transaction_type><ssl_card_number>1234567890123456"+
"</ssl_card_number><ssl_exp_date>1617</ssl_exp_date><ssl_amount>1.00"+
"</ssl_amount></txn>", HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppost);
String response_string = EntityUtils.toString(httpresponse.getEntity());
Log.d("request", response_string);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The response which I got is below
<?xml version="1.0" encoding="UTF-8"?>
<txn><errorCode>6042</errorCode><errorName>Invalid Request Format</errorName><errorMessage>XML request is not well-formed or request is incomplete.</errorMessage></txn>
Please suggest me something how to get rid off this issue. I heard about SOAP but don't know how to use that. Any help is appreciated.
If you are sure that your XML is what the server expects, I suggest trying to use RequestMaker to see if the problem is really Android related. You can also modify encoding and HTTP header elements to test various options.
I think that your intention is to use SOAP for a Web service request, but your XML fragment is definitely not SOAP! Find here a step by step guide to calling a SOAP Web service: LINK
Related
I know that this is a duplicate Question. But i didn't get the proper answer. My question is that. I have some data and i want to convert that data into xml and i want to send this xml with HttpPost request. When This Post request is executed then it give me data in xml format. then i want to parse the xml data. Please tell me the best way to do it. I have read Some tutorial but I haven't get proper answer. is there no other way to convert object value into xml accounting to the class field Like marshaling and unmarshaling in java please tell me the answer. Thanks in advance. I have read some example here are some links. click here and here
Simple example for XML parser using HTTP POST,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.192.131/");
try {
StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
tvData.setText(EntityUtils.toString(resEntity));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have a sample url like this:
http://www.sample.com/mobile.cgi?action=login&json_request={"user":{"name":"a","pass":"123","gender":"m","age":"25"}}
I can basically use webview.loadurl to send data to the server, but the point is...I cant get response from the server. I'm new to json. Is there any way that I can post json using the regular way? like HttpPostmaybe? and be able to get response properly.
Thanks!!
If you just wish to post JSON using HTTP and get a response back, there are many posts of stackoverflow which will help you answer that. Check How to send POST request in JSON using HTTPClient? question. I think this question answers what you are trying to say. Hope this helps you. If you have any specific concern you can always comment.
Update
As you said that you already have keys and corresponding values in addition to URL.
The first step would be to create a JSON Object. Convert it to string and then you can send it using HTTPClient and get the response back. Something like:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/");
try {
// Add your data
JSONObject user = new JSONObject();
user.put("Name", "a");
user.put("pass", "123");
// Create StringEntity
StringEntity se = new StringEntity( user.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
You can check links like this to see further exact format. I wanted to tell you the method as to how you can proceed. Hope this helps.
I keep getting the error above (title) when I try to make a HTTP-request to a server with a special character (the character 'å'). I have tried to call:
_login = URLEncoder.encode(_login, "utf-8");
But I still get exception. If I try to change the URL it works fine. Seems that it happends for whatever URL I try if it has special characters. Like for example http://www.ål.no.
Anyone know about a work-around? One way could of course be to use the IP-address. But I would rather avoid that.
Thanks for any help!
Some of the source code:
private String _login = "http://www.ål.no";
HttpClient httpclient = new DefaultHttpClient();
try {
_login = URLEncoder.encode(_login, "utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpPost httppost = new HttpPost(_login);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("Mail", this.Email));
//nameValuePairs.add(new BasicNameValuePair("Password", this.Password));
try
{
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
BasicHttpResponse response = null;
try
{
response = (BasicHttpResponse) httpclient.execute(httppost);
}
catch (ClientProtocolException e)
{
System.out.println("BasicHttpResponse");
e.printStackTrace();
}
}
EDIT: Found a work around. Used Firebug to dig a little deeper. According to Firebug the server has another name when communicating (as far as I can see). This name does not contain any special characters and I successfully managed to communicate with the server using my application. Thanks for all help! :)
I think I understand what you're asking for. Here is a link to a list of special characters that the URL will identify.
http://www.degraeve.com/reference/urlencoding.php
If thats not what you need let me know.
to save you time, the character you're searching for is %E5
I want to send the JSON text {} to a web service and read the response. How can I do this from android? What are the steps such as creating request object, setting content headers, etc.
My code is here
public void postData(String result,JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
String json=obj.toString();
try {
HttpPost httppost = new HttpPost(result.toString());
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
Log.i("tag", temp);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
what mistake i have done plz correct me because it shows me an bad request error
but when i do post in poster it shows me status as Successfull 200 ok
I do this with
httppost.setHeader("Content-type", "application/json");
Also, the new HttpPost() takes the web service URL as argument.
In the try catch loop, I did this:
HttpPost post = new HttpPost(
"https://www.placeyoururlhere.com");
post.setHeader(HTTP.CONTENT_TYPE,"application/json" );
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("json", json));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
response = EntityUtils.toString(entity);
You can add your nameValurPairs according to how many fields you have.
Typically the JSON might become really huge, which I will then suggest gzipping it then sending, but if your JSON is fairly small and always the same size the above should work for you.
If it is a web service and not RestAPI call then, you can get the WSDL file from the server and use a SOAP Stub generator to do all the work of creating the Request objects and the networking code for you, for example WSClient++
If you wish to do it by yourself then things get a little tricky. Android doesn't come with SOAP library.
However, you can download 3rd party library here: http://code.google.com/p/ksoap2-android/
If you need help using it, you might find this thread helpful: How to call a .NET Webservice from Android using KSOAP2?
If its a REST-API Call like POST or GET to be more specific then its is very simple
Just pass a JSON Formatted String object in you function and use org.json package to parse the response string for you.
Hope this helps.
There is a relevant question, but I could not get the answer clearly.
I would like to POST a short xml code
<aaaLogin inName="admin" inPassword="admin123"/>
to a specific URL address over HTTP. The Web service will send me back a XML code. The important part is that I will parse the received XML, and I want to store that as a file.
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.192.131/"); //URL address
StringEntity se = new StringEntity("<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>",HTTP.UTF_8); //XML as a string
se.setContentType("text/xml"); //declare it as XML
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient .execute(httppost);
tvData.setText(httpResponse.getStatusLine().toString()); //text view is expected to print the response
there is something wrong with receiving the response. Besides, I did not write anything to save the received XML as a file. Can someone write a code snippet?
Ok, I have figured out soon after I posted this question.
This code here works fine:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.192.131/");
try {
StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
tvData.setText(EntityUtils.toString(resEntity));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You can get the content of the response using:
String responseXml = EntityUtils.toString(httpResponse.getEntity());
You can then write this to a file using something like this.
there is something wrong with receiving the response
Since you havn't said what is wrong with receiving the response it's somewhat difficult to help you with this point.
Why not use Spring RestTemplate in Spring for Android?