How to add OkHttp's deprecated code in eclipse? - java

I am new with Android. Please guide me. Here is my code. How can i add or change the code with OkHttp?
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//HttpClient httpclient = new DefaultHttpClient();
HttpClient httpclient =HttpClientBuilder.create().build();
try{
HttpPost httppost = new HttpPost("http://10.0.2.2/hari/test.php");
StringEntity se = new StringEntity("envelope",HTTP.UTF_8);
httppost.setEntity(se);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
I had problems when I am using the deprecated code. How can i use the OkHttp in my code? I do try include OkHttp in my code but there are more errors detected. I am using android API 20 and above. I also got warning for utf_8.
What should i do?

Related

How to resolve org.apache.http.conn.ConnectTimeoutException in Android?

I am working on service related application.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000000);
HttpConnectionParams.setSoTimeout(params, 15000000);
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),true);
String str_Server_Host = Control_PWD_Server_Name();
String requestEnvelope = String.format(envelope_oe);
// HttpPost httpPost = new HttpPost(str_Server_Host);
HttpPost httpPost = new HttpPost(str_Server_Host);
httpPost.setHeader("SOAPAction",
"http://tempuri.org/updateTransportStatus");
httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
httpPost.setEntity(new StringEntity(requestEnvelope));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
String str = Html.fromHtml(line).toString();
This is the exception I am getting:
org.apache.http.conn.ConnectTimeoutException: Connect to /10.64.0.184:9092 timed out
How to resolve above exception in android? please give any suggestion or related senario.

Why does an HttpGet works without timeouts but with timeouts causes an internal server error?

I don't know how the HttpClient works exactly but I find it quite strange that I get an internal server error if I initialise the httpclient with new DefaultHttpClient(httpParameters) but everythings works fine if I initialise it with new DefaultHttpClient(). I should additionally mention that the error does not occur on the first request. Here's a piece of my code, are there any errors?
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpclient.execute(new HttpGet(url));
int statusCode = response.getStatusLine().getStatusCode();
Try to change
HttpClient httpclient
to
DefaultHttpClient httpclient

Adding parameter to HttpPost on Apache's httpclient

I am trying to set some Http parameters in the HttpPost object.
HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);
It looks like the parameter is not set at all. Do you have any idea why this is happening?
Thank you
For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :
StringBuilder requestUrl = new StringBuilder("your_url");
String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());
NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);

java- unknown host exception when using apache http client

I am trying to make a simple GET request for a website, but I am getting unknown host exception.
Given below is my code--
DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost=null;
targetHost= new HttpHost("google.com/", 80, "http");
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localcontext = new BasicHttpContext();
try {
HttpResponse response = client.execute(targetHost, httpget, localcontext);
It looks like you have a simple problem here.
The URL for your 'HttpHost' object is malformed. You need to drop the '/' from "google.com/".
It should work after that. I used your code with that single modification & it worked.
DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("google.com", 80, "http");
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localContext = new BasicHttpContext();
HttpResponse response = null;
try { response = client.execute(targetHost, httpget, localContext);
System.out.println(response.getStatusLine()
}
catch(Exception e){
// Enter error-handling code here.
}

How to Handle the Session in Apache HttpClient 4.1

I am using the HttpClient 4.1.1 to test my server's REST API.
I can manage to login seem to work fine but when I try to do anything else I am failing.
Most likely I have a problem setting the cookie in the next request.
Here is my code currently:
HttpGet httpGet = new HttpGet(<my server login URL>);
httpResponse = httpClient.execute(httpGet)
sessionID = httpResponse.getFirstHeader("Set-Cookie").getValue();
httpGet.addHeader("Cookie", sessionID);
httpClient.execute(httpGet);
Is there a better way to manage the session/cookies setting in the HttpClient package?
The correct way is to prepare a CookieStore which you need to set in the HttpContext which you in turn pass on every HttpClient#execute() call.
HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// ...
HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...
HttpResponse response2 = httpClient.execute(method2, httpContext);
// ...

Categories