get the HTTP status server - android

Hi I am developing an android application, I like to create a class to get the HTTP status before send the data to the server with HTTP Post.
Have any form to get the HTTP status of this server?
I read to get the 200 code is the server is running and another code no
Thanks.
Resolved the timeout is very long, My solution is:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
HttpClient httpclient = new DefaultHttpClient(params);
and then:
HttpGet httpRequest = new HttpGet(server);
HttpResponse response = httpclient.execute(httpRequest);

You could do the following
HttpGet httpRequest = new HttpGet(myUri);
HttpEntity httpEntity = null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
response.getStatusLine().getStatusCode()

This is how you get Response code if you are using HttpUrlConnection :
when server is not running
int status = ((HttpURLConnection) connection).getResponseCode();
Log.i("", "Status : " + status);
And here is if you are using HttpClient :
HttpResponse response = httpclient.execute(httppost);
Log.w("Response ","Status line : "+ response.getStatusLine().toString());

Related

how to send object over Get Request in android

I want to send the below JSON request to a web service and read the response.
{"Email":"aaa#tbbb.com","Password":"123456"}
I know to how to read JSON. The problem is that the above JSON object must be sent in a variable name json.I want to send Json object as a parameter with get request.
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpGet httpget = new HttpGet(FinalURL.toString());
//HttpPost request = new HttpPost(serverUrl);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
How can I do this from android? What are the steps such as creating request object, setting content headers, etc.
You can set request using:
httpget.setEntity(new JSONObject(requestString));
One of the options i'm using right now is RequestParams:
It can be implemented as:
RequestParams rp = new RequestParams();
rp.put("Email", "aaa#tbbb.com");
rp.put("Password", "123456");
Utils.client.get(url, rp, new AsyncHttpResponseHandler() {
//or your http code
http://loopj.com/android-async-http/doc/com/loopj/android/http/RequestParams.html
http://loopj.com/android-async-http/

Android: HttpClient, cannot retry request with a non-repeatable request entity

Trying to send file content to server from Android application like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
final InputStreamEntity reqEntity = new InputStreamEntity(
gdFileSystemDelegate.openFileInput(openFileInput(FilePath), -1);
reqEntity.setContentType("application/octet-stream");
httppost.setEntity(reqEntity);
httpClient.execute(httppost);
But its throws an exception:
cannot retry request with a non-repeatable request entity
What does it mean ? how to fix that ?
Try to set protocol param http.protocol.expect-continue to true in DefaultHttpClient:
#Override
protected HttpParams createHttpParams() {
HttpParams params = super.createHttpParams();
HttpProtocolParams.setUseExpectContinue(params, true);
return params;
}

Android : call httpget with redirect_uri

I want to make httpget request by android application,
URL = https://www.facebook.com/dialog/oauth?client_id=<number>&redirect_uri=<SOME_URL>&scope=email
Above URL is working fine with browser, It give me proper result on server side, but when I am making http call from application it won't work, got the 200 Response, but it won't give me result.
Code snippet:
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParams,
CONN_TIMEOUT);
HttpGet httpGet = new HttpGet(URL);
Log.d(TAG,"URL :"+ httpGet.getURI().toURL().toString());
DefaultHttpClient client = new DefaultHttpClient(httpParams);
HttpResponse httpResponse = client.execute(httpGet);
//Log.d(TAG,"httpResponse :" +EntityUtils.toString(httpResponse.getEntity()));
res = httpResponse.getStatusLine().getStatusCode();
Log.d(TAG, "Response : " + res);
this is because
200 response code "OK"
The request has succeeded. The information returned with the response is dependent on the method used in the request,if there is some result request code will 204

What is wrong with this POST request with json on android?

I'm trying to perform a POST request to a server that wants the Content-Type set to application/json with name and email as some keys. Currently, I'm getting a 406 error, which I'm assuming is working on the server side, but android can't handle the response. How can I tweak the code to get a 200 response?
HttpClient client = new DefaultHttpClient();
HttpEntity entity;
try{
JSONObject j = new JSONObject();
j.put("name" , myName);
j.put("email", myEmail);
HttpPost post = new HttpPost(targetURL);
post.setHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(j.toString(), HTTP.UTF_8);
se.setContentType("application/json");
post.setEntity(se);
HttpResponse response = client.execute(post);
entity = response.getEntity();
Log.d("response", response.getStatusLine().toString());
} catch(Exception e){Log.e("exception", e.toString());}
Does that look about right? Do I need one of those response handlers when creating the HttpClient?
This works for me with json-2.0.jar
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), MyApplication.HTTP_TIMEOUT); //Timeout Limit
HttpResponse response;
ArrayList<appResults> arrayList = new ArrayList<appResults>();
String resul;
try{
HttpGet get = new HttpGet(urls[0]);
response = client.execute(get);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
resul = convertStreamToString(in);
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<appResults>>() {}.getType();
arrayList = gson.fromJson(resul, listType);
in.close();
of course in asynctask or thread.
But 406... it seems that your format on your webserver and your app are not consistent...

Android: How get the status-code of an HttpClient request

I want to download a file and need to check the response status code (ie HTTP /1.1 200 OK).
This is a snipped of my code:
HttpGet httpRequest = new HttpGet(myUri);
HttpEntity httpEntity = null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
...
How do i get the status-code of the response?
This will return the int value:
response.getStatusLine().getStatusCode()
if (response.getStatusLine().getStatusCode()== HttpsURLConnection.HTTP_OK){
...
}
Use code() function of response to get its HTTP code:
val code = response.code()

Categories

Resources