Consuming Restful WCF Service using Android [duplicate] - java

This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
NetworkOnMainThreadException [duplicate]
(5 answers)
Closed 8 years ago.
I have been attempting to to connect to a WCF Service from a Android device. I have read a lot of blogs that does not seem to be useful. One of the Operations running on my WCF is
[OperationContract]
[WebGet(UriTemplate = "write", ResponseFormat = WebMessageFormat.Json)]
string write();
This writes one entity to a database. When I enter the URL in my phones browser "10.0.0.14/serv/UserManagement.svc/write" I get the relevant message and it writes to the database with no problem. The problem arises when I attempt to Consume the WCF from a android application. I have jumped between many different solution types and I am currently using
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri = new URI("http://10.0.0.14/serv/UserManagement.svc/write");
HttpGet httpget = new HttpGet(uri);
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-type", "application/json; charset=utf-8");
HttpResponse response = httpClient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
}
catch (Exception e)
{
e.printStackTrace();
}
This does not work. I have added <uses-permission android:name="android.permission.INTERNET"/> to my manifest. In my LogCat there is a NetworkOnMainThreadException. How can I fix the problem?

Related

Yahoo Currency Converter API [duplicate]

This question already has answers here:
Has Yahoo suddenly today terminated its finance download API?
(4 answers)
Closed 5 years ago.
I've been using the Yahoo Currency Converter all along without issues.
Here is the function code in Java:
public static Float convert(String currencyFrom, String currencyTo) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpGet, responseHandler);
httpclient.getConnectionManager().shutdown();
return Float.parseFloat(responseBody);
}
However, just yesterday I realised it was throwing the following error:
It has come to our attention that this service is being used in
violation of the Yahoo Terms of Service. As such, the service is being
discontinued. For all future markets and equities data research,
please refer to finance.yahoo.com.
Is there some problems with the code I'm using? Or has the service been discontinued permanently. Any alternative suggestion for real time currency conversion?
I can confirm that the service has been discontinued overnight.
https://forums.yahoo.net/t5/Yahoo-Finance-help/http-download-finance-yahoo-com-d-quotes-csv-s-GOOG-amp-f/m-p/387662/highlight/true#M6207
This is the answer from the admin of the community site.
Although discontinued, you could look at an alternative such as http://fixer.io, which would allow you to do something similar via JSON
https://api.fixer.io/latest?base=currencyFrom&symbols=currencyTo

HttpClient is deprecated (android studio, Target=api 22) [duplicate]

This question already has answers here:
Deprecated Java HttpClient - How hard can it be?
(10 answers)
Closed 7 years ago.
In my Android app it says: org.apache.http.client.httpclient is deprecated.
After some research I found out that Android has deprecated it in API 22. I have searched in the forum and tried: "The application has stopped", Searched google: "The application has stopped". So I have no idea what to do. I hope you guys can help me out. Well here is my code:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.URL.com");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success");
// Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_LONG).show();
EDIT:
I'm trying to get some data of my Mysql database. Do you know a good tutorial about how this is done?
Please let me know.
Stop using it and use URLConnection instead. It's been 4 years Google recommends this.
http://android-developers.blogspot.be/2011/09/androids-http-clients.html
If you want an external library with a nicer API, you can try OkHttp: http://square.github.io/okhttp/

When should the HttpClient be closed? [duplicate]

This question already has answers here:
What is the difference between CloseableHttpClient and HttpClient in Apache HttpClient API?
(8 answers)
Closed 2 years ago.
I am building an Android app that will fire multiple HTTP requests (say a request every second) to a server to fetch data. What are the best practices I must follow?
Should I create and close the client after each request, like the following?
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://yoururl");
StringEntity params = new StringEntity(json.toString());
request.addHeader("content-type", "application/json");
request.setEntity(params);
httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.close();
}
Or should I create a client initially, use it for all requests and then finally close it when I'm done with it?
The idea of closing your HttpClient is about releasing the allocated ressources. Therefore, It depends on how often you plan on firing those HTTP requests.
Keep in mind that firing a request every 10 seconds is considered an eternity ;)

Large data is not sending in android through webservice

I am using the following code in android to send data to a server through a web service
call.When i am sending small amount of data it is hitting the server.When i am sending large data it is not hitting the server.Simply it is httpClient.execute(httpPost); .But i am not getting any result.What might be the problem
HttpPost httpPost = new HttpPost(url+data);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (rsopnse != null)
System.out.println(httpPost.getMethod());
try
{
httpResponse= httpClient.execute(httpPost);
}catch(Exception e)
{
e.printStackTrace();
}
Thanks in advance...
You need to create a List<NameValuePair> for all the parameters that you want to pass in the Request. You should not append your parameters to the URL, which is more of the GET style of making a call.
The examples for HTTP Post are covered in the post here.

Android can't access a web server?

So I've got a little script set up on my server that says whether a user is log in able or not. When accessing the url http:server-url/username/password/ I would get a string returned called "correct" or "incorrect".
What I'm doing in my Android app is the following:
HttpClient httpclient = new DefaultHttpClient();
String url = "http://server-url/"+usernameText+"/"+passwordHash+"/";
HttpPost httppost = new HttpPost(url);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
String response = httpclient.execute(httppost, handler);
Log.e("logged in",response);
} catch (ClientProtocolException e) {
Log.e("clientprotocolexception",e.toString());
} catch (IOException e) {
Log.e("ioexception","error");
}
I'm getting an error:
04-29 14:31:15.728: ERROR/clientprotocolexception(9366): org.apache.http.client.HttpResponseException: FORBIDDEN
but the website itself works fine when I just access it through the browser? Are these some settings I have to set correctly somewhere on the server, or am I forgetting something in my code?
Thanks!
Did you think of giving the internet permission?
<uses-permission android:name="android.permission.INTERNET"/>
I was using Post instead of Get, JCs answered it in the comments on my original question.
Use this example to guide you to pass username password and credentials, instead of using the url.

Categories

Resources