Android : call httpget with redirect_uri - android

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

Related

Android :HttpGet with Params unsuccessful

I am using Apache HttpClient, DefaultHttpClient, HttpResponse to make HttpGet Request with JsonObject as request parameter. I receive response in JsonObject format.The Content-Type is application/json
The problem is once a login request fails by providing incorrect login details the next time you correct the details you always fail to get success response.
This scenario doesn't happen if the very first login is successful.
I suspect the params are not going through correctly although I have checked the same. Is there a possibility that the previous request is cached and the new params are not going through.
JSONObject inputBodyObject = new JSONObject();
inputBodyObject.put("email", emailId);
inputBodyObject.put("password", password);
String strURL = this.getString(R.string.base_url) + this.getString(R.string.action_login) + Uri.encode(inputBodyObject.toString());
HttpGet httpGet = new HttpGet(strURL);
httpGet.setHeader("Content-type", "application/json"));
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();

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/

get the HTTP status server

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());

Response is fires an Exception though the url and request codes are correct

In code to connection
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
when it comes to response execute it's just fires an exception
java.lang.nullPointerException
i checked every thing i.e
"url receiving and passing to request.setURI() method, the url is ok but it still crashes on response Line"

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