I have .net web service that expect JSON object. This web service has method for return token:
public string GetToken(string username, string password)...
This is my site url for direct access (when I field manually id, method and params and paste in browser url I receive response )
http://mysite.com/JsonRPC.aspx?id={0}&method={1}¶ms={2}
On stackoverflow I found way to create and send JSON object in android , here is example:
HttpPost request = new HttpPost(URL);
JSONStringer json = new JSONStringer()
.object()
.key("username").value("username")
.key("password").value("password")
.endObject();
Log.i("json",json.toString());
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
My problem is that I don't know how to call my .net web service. What format should be URL variable , where to specific method name , and how to specific method parameters ?
Please give me code
Thanks
I would use this library.
http://code.google.com/p/android-json-rpc/
There are examples on the website that should be sufficient for you.
Related
I am transitioning an existing service from using google url shortener api to try and use Firebase Dynamic Links. I have linked a project from the Google Cloud Platform, and setup a "dummy" android app so that I can have the app domain for the dynamic links. I am trying to use the REST API to shorten urls for very long urls that can't be handled by a third party. I have tried sending using:
ObjectMapper mapper = new ObjectMapper();
HttpPost httpPost = new HttpPost("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=****");
FirebaseDynamicLinkInfo dynamicLinkRequest = new FirebaseDynamicLinkInfo();
dynamicLinkRequest.setDynamicLinkDomain("zw5yb.app.goo.gl");
dynamicLinkRequest.setLink(assetUrl);
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(dynamicLinkRequest)));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
responseBody = httpClient.execute(httpPost, responseHandler);
I am getting a 400 Bad Request when I post the request to the API (on the httpCLient.execute line. I have double checked my api-key. I have also tried using just the longDynamicLink parameter, and it gets the 400 Bad Request Response.
Any ideas of where I could be going wrong?
Thanks,
Ben
I contacted Google Support on this one, and I wasn't UrlEncoding my querystring parameters on the deep link. After encoding the link, the request was successful. I went back to using passing json that just had a longDynamicLink property (as opposed to the dynamicLinkInfo object in my original post). Here is what it looks like:
String myEscapedUrl = "https://zw5yb.app.goo.gl/?link=" + URLEncoder.encode(assetUrl, "UTF-8");
FirebaseDynamicLinkRequest dynamicLinkRequest = new FirebaseDynamicLinkRequest(myEscapedUrl);
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(dynamicLinkRequest)));
// inform the server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
responseBody = httpClient.execute(httpPost, responseHandler);
POST request to server using java URLConnnection
I need to send a POST request with the two parameters below:
param1=value1
param2=value2
And also I need to send a file.
In the case of Apache these 2 two(sending params and file) things are handled like below
post.setQueryString(queryString) // queryString is url encoded for eg: param1=value1¶m2=value2
post.setRequestEntity(entity) // entity is constructed using file input stream with corresponding format
Please let me know if you have anything related to this problem.
Please note: When I try using Google Chrome REST client plug-in, I am getting the response as below (tried with all request content-types)
UNSUPPORTED FILE FORMAT: 'multipart/form-data' is not a supported content-type
Response code is 400.
Try this API from Apache to send request internally with POST method.
The below is the sample Code to use API
List<org.apache.http.NameValuePair> list =new ArrayList<org.apache.http.NameValuePair>();
HttpPost postMethod = new HttpPost("http://yoururl/ProjectName");
list.add(new BasicNameValuePair("param1", "param1 Value")) ;
postMethod.setEntity(new UrlEncodedFormEntity(list));
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(postMethod);
InputStream is = response.getEntity().getContent();
I want to write a Java program to call a web service. WSDL is not available for this web service. I have written programs to call a web service which has wsdl. Here I don't have any idea of how I can proceed. Not able to find many samples in Internet as well.
Is there any better frame work which I can use? I am getting JSON output from web service.
I am looking at options of writing a best possible case(If I could write a generalized program which could be used for many web services with out much changes, it would be great)
Well there are several ways to consume rest service.
Using Spring framework:
import org.springframework.web.client.RestTemplate
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject("http://localhost:8080/users/2", User.class);
System.out.println("Username: " + user.getUsername());
Using apache httpclient:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://localhost:8080/users/2");
HttpResponse response = httpClient.execute(getRequest);
HttpEntity httpEntity = response.getEntity();
String userString = EntityUtils.toString(httpEntity);
// Transform 'userString' into object using for example GSON:
Gson gson = new Gson();
User user = gson.fromJson(userString, User.class);
System.out.println("Username: " + user.getUsername());
I am trying to update email alias for different users. I am able to authenticate, get the code and then get the access token. I am sending the access token in the HTTP POST request as a Header. I am using Java & Apache HTTPClient to make the RESTful call. Here is the code snippet (Only relevant code shown).
if (httpClient != null) {
String apiURL = getApiURL();
apiURL = MessageFormat.format(apiURL, "firstname.lastname#company.com");
// apiURL = https://api.box.com/2.0/users/firstname.lastname#company.com/email_aliases
// firstname.lastname#company.com does exist in the Box Account
HttpPost post = new HttpPost(apiURL);
post.addHeader("Authorization", "Bearer "+accessToken);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "updateemail#company.com"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.defaultCharset()));
HttpEntity entity = post.getEntity();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseFromBox = httpClient.execute(post, responseHandler);
writeResponse(response, responseFromBox);
if (responseFromBox != null) {
if (logger.isDebugEnabled()) {
logger.debug("apiURL-->"+apiURL);
logger.debug(responseFromBox);
}
}
}
The problem is that the response I get is some HTML code that says "The page you were viewing has expired. Please go back and try your request again." I was expecting some JSON string.
What I am doing incorrect? In the Post request instead of sending the email address I used the user id. But I get the same error.
In fact when I try to fetch the email alias of a user using the HTTP GET request I get an error "Not Found". The user does exist. I have an admin control. I can see them.
Thanks
Raj
Try a get on /users to get the array of all your users in the enterprise first. Is that working for you? If not, can you do a get on /users/me? If you can't get the former, then your API key may not have the "manage an enterprise" grant setup for it. You have to set that up in the app management, where you setup your OAuth2 callback URL.
Not sure why you are getting HTML back. That usually only happens on badly formed requests that our servers can't even parse, like you are hitting the wrong URL.
Just a reminder, OAuth2 URL is different from the API URL. 1st is https://www.box.com/api/oauth2/.... 2nd is https://api.box.com/2.0/...
As for setting the Email alias, that's entirely possible, once you know the ID of the user you are trying to set the alias for. Documentation is here
I was using the NameValuePair instead of the JSON string that was being expected. So I removed the following
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "updateemail#company.com"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.defaultCharset()));
and added
String json = "{\"email\":\"firstname.lastname#company.com\"}";
StringEntity entity = new StringEntity(json, Charset.defaultCharset());
post.setEntity(entity);
and then things started to work!
I'm trying to access this link for web service via android .
but I don't know what is library or jar files should using to do this .
I have used soap but not working will
here is link is :
http://ictfox.com/demo/Hafil_Updates/Login_Check.aspx?UserLogin=Demo&Password=Demo
I think you can use DefaultHttpClient android api to send GET request as you shown.
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI webserv = new URI(" http://ictfox.com/demo/Hafil_Updates/Login_Check.aspx?UserLogin=Demo&Password=Demo");
request.setURI(webserv);
HttpResponse response = httpclient.execute(request);
String responseStr = EntityUtils.toString(response.getEntity());
this is tutorial show you how to call android application webservice using protocol soap
http://android.programmerguru.com/how-to-call-asp-net-web-service-in-android/