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/
Related
This question already has answers here:
How To Send json Object to the server from my android app
(4 answers)
Closed 3 years ago.
I have a server to which I need to send a request and get a response.
I am using volley and this is what I have done so far
JSONObject request=new JSONObject();
try {
request.put("ticket_id", ticket_id);
request.put("start", start);
request.put("extra", extra);
request.put("care_category", care_category);
request.put("k_means", "10");
request.put("vendor", vendor);
request.put("earlier_enteries", earlier_enteries);
} catch (JSONException e) {
e.printStackTrace();
}
The response would be something like :
{"calculation":5.4444"}
How should I rend my request and or if there is any better way to do so ?
You have to use HTTPs requests (GET and POST). Retrofit and Gson will get it done.
There's a lot of tutorials and examples about it.
See Retrofit Android Example HTTP GET Request
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
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 ;)
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?
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.