Waiting a response in Thread via HttpUrlConnection - java

I have a Function which sends url request to server and gets data. I'm able to get data but data is coming late as request is being sent in another Thread.
I want user to wait for the request (For login authentication). How do achieve this?
private String sendRequest(final String URL) {
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
HttpURLConnection httpUrlConnection = null;
try {
httpUrlConnection = (HttpURLConnection) new URL(URL)
.openConnection();
InputStream in = new BufferedInputStream(
httpUrlConnection.getInputStream());
data = readStream(in);
Log.i(TAG,"Respose from "+URL);
System.out.println(data);
} catch (MalformedURLException exception) {
Log.e(TAG, "MalformedURLException");
} catch (IOException exception) {
Log.e(TAG, "IOException");
} finally {
if (null != httpUrlConnection)
httpUrlConnection.disconnect();
}
}
}).start();
return data;
}
I call above function like this
public JSONObject get(String URL){
data = sendRequest(URL);
try {
if(null!=data){
returnJsonObj = new JSONObject(data);
}else{
Log.i(TAG,"Response data is null");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnJsonObj;
}
How do I wait for a data in get Function.

Related

How can I get youtubeVideo Title from URL for android studio?

I want to get the youtube video title from a url so I found this code below (IOUtils) is depreciated any other way to do this
public class SimpleYouTubeHelper {
public static String getTitleQuietly(String youtubeUrl) {
try {
if (youtubeUrl != null) {
URL embededURL = new URL("http://www.youtube.com/oembed?url=" +
youtubeUrl + "&format=json"
);
return new JSONObject(IOUtils.toString(embededURL)).getString("title");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
second way i tried
class getYoutubeJSON extends Thread {
String data = " ";
#Override
public void run() {
try {
URL url = new URL("http://www.youtube.com/oembed?url="+" https://www.youtube.com/watch?v=a4NT5iBFuZs&ab_channel=FilipVujovic"
+ "&format=json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null){
data =data + line;
}
if(!data.isEmpty()){
JSONObject jsonObject = new JSONObject(data);
// JSONArray users = jsonObject.getJSONArray("author_name");
Log.d("RT " , jsonObject.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
This code gets a an error Cleartext HTTP traffic to www.youtube.com not permitted
so I found this answer Android 8: Cleartext HTTP traffic not permitted but I am still getting some error I don't understand.
I solved this problem by using the volley library.
My requested url was:
String Video_id = "jhjgN2d7yok";
String url = "https://www.youtube.com/oembed?url=youtube.com/watch?v=" +Video_id+ "&format=json";

Java/Yahoo Finance Retrieving Quotes, Error 401

Here's my Java code to download historical Yahoo finance quotes using crumbs. Sorry, it's not the most efficient code. I'm still learning Java. Everything looks correct to me, but I get error 401 after picking up the crumb and trying to make the second access to "https"//query1.finance.yahoo.com..."
Any ideas of what I'm doing wrong?
URL myUrl = null;
try {
myUrl = new URL("https://finance.yahoo.com");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
URLConnection urlConn = null;
try {
urlConn = myUrl.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
urlConn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String headerName=null;
String crumb = null;
for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = urlConn.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
crumb = cookie.substring(cookie.indexOf("=") + 1, cookie.indexOf("&"));
}
}
StringBuilder qUrl = new StringBuilder();
qUrl.append("https://query1.finance.yahoo.com/v7/finance/download/AAPL");
qUrl.append("?period1=" + 1495813803L);
qUrl.append("&period2=" + 1497887408L);
qUrl.append("&interval=1d");
qUrl.append("&events=history");
qUrl.append("&crumb=" + crumb);
URL url = null;
try {
url = new URL(qUrl.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection conn;
InputStream is = null;
try {
conn = url.openConnection();
String redirect = conn.getHeaderField("Location"); // Can handle one redirection
if(redirect != null ) {
conn = new URL(redirect).openConnection();
}
is = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}

What is the proper way of Exception Handling in the following scenario:

I have an Async task which checks for user session. This task is responsible for contacting the server using a method - both are listed below:
public class SessionChecker extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Intent intent;
if (result.equalsIgnoreCase("exception")) {
//We got an exception in URL Connection - letus restart the task -- this is recursive
new SessionChecker().execute();
} else if (result.equalsIgnoreCase("server error")) {
//We got an exception in URL Connection - letus restart the task -- this is recursive
new SessionChecker().execute();
} else {
try {
JSONObject jObj = new JSONObject(result);
if (!TextUtils.isEmpty(result)) {
String message = jObj.getString("message");
if (message.equalsIgnoreCase("failure")) {
intent = new Intent(AppInitializer.this, AppWebViewLogin.class);
startActivity(intent);
} else if (message.equalsIgnoreCase("success")) {
intent = new Intent(AppInitializer.this, NavigationMasterActivity.class);
startActivity(intent);
}
}
} catch (JSONException ex) {
//Malformed JSON - letus restart the task -- this is recursive
new SessionChecker().execute();
}
}
}
#Override
protected String doInBackground(Void... params) {
DomainManager domainManager = new DomainManager(AppInitializer.this);
return UniversalNetworkConnection.simplePost(domainManager.getDomain() + getResources().getString(R.string.url_check_session));
}
}
Here is my URL Connection method:
public static String simplePost(String myurl) {
HttpsURLConnection conn = null;
try {
SSLContext sslcontext = SSLContext.getInstance("TLSv1");
sslcontext.init(null, null, null);
SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);
StringBuffer response;
URL url = new URL(myurl);
conn = (HttpsURLConnection) url.openConnection();
// conn.setReadTimeout(90000);
// conn.setConnectTimeout(900000);
conn.setRequestProperty("Content-Type", "application/json");
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(new URL(myurl).getHost());
conn.setDoOutput(true);
conn.setRequestProperty("Cookie", cookie);
conn.setRequestMethod("POST");
int responseCode = conn.getResponseCode();
switch (responseCode) {
case 200:
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
default:
return "server error";
}
} catch (IOException | java.security.KeyManagementException | java.security.NoSuchAlgorithmException ex) {
ex.printStackTrace();
return "exception";
} finally {
if (conn != null) {
try {
conn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
return "exception";
}
}
}
}
I have tried handling the exceptions recursively - an exception occurs - The async task is reloaded, how can I improve this, I am not sure if the way I am doing this is the best practice? Any hints / solutions / tips would be really helpful.

Android - How to Handle an Async Http Crash

My app is currently crashing whenever it cannot connect to the server. How do I handle this, and instead let the user know that the server is down and to try again.
private void sendPostRequest(String givenEmail, String givenPassword) {
class SendPostRequestTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String emailInput = params[0];
String passwordInput = params[1];
String jsonUserInput = "{email: " + emailInput + ", password: "
+ passwordInput + "}";
try {
HttpClient httpClient = new DefaultHttpClient();
// Use only the web page URL as the parameter of the
// HttpPost argument, since it's a post method.
HttpPost httpPost = new HttpPost(SERVER_URL);
// We add the content that we want to pass with the POST
// request to as name-value pairs
json = new JSONObject(jsonUserInput);
jsonString = new StringEntity(json.toString());
httpPost.setEntity(jsonString);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpParams httpParameters = httpPost.getParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// HttpResponse is an interface just like HttpPost.
// Therefore we can't initialize them
HttpResponse httpResponse = httpClient.execute(httpPost);
// According to the JAVA API, InputStream constructor does
// nothing.
// So we can't initialize InputStream although it is not an
// interface
InputStream inputStream = httpResponse.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
Log.i(LOGIN, "ClientProtocolException");
cpe.printStackTrace();
} catch (ConnectTimeoutException e) {
e.printStackTrace();
} catch (IOException ioe) {
Log.i(LOGIN, "IOException");
ioe.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i(LOGIN, result);
try {
serverResponse = new JSONObject(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if ((serverResponse.has("status"))
&& (serverResponse.get("status").toString()
.equals("200"))) {
Toast.makeText(getApplicationContext(), "SUCCESS!",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Incorrect Email/Password!!!",
Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SendPostRequestTask sendPostRequestTask = new SendPostRequestTask();
sendPostRequestTask.execute(givenEmail, givenPassword);
}
LogCat Error Log
11-11 16:26:14.970: I/R.id.login_button(17379): IOException
11-11 16:26:14.970: W/System.err(17379): org.apache.http.conn.HttpHostConnectException: Connection to http://* refused
11-11 16:26:14.980: W/System.err(17379): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
I can see that you are already catching the Exceptions and have a String as parameter type to onPostExecute. From inside the exceptions, you can pass a string like "error" to the onPostExecute, whenever an error occurs. Inside the onPostExecute you can check:
if the string is equal to "error":
then create a Alert dialog box from within `onPostExecute` and show it.
else:
continue as desired
Ideally a boolean would do the trick but since you already have a string, you can also use that. Otherwise you can have a struct with a string and a boolean and then pass it to onPostExecute. Hope it gives you the idea.
Or you can create new Object
public class AsyncTaskResult<T> {
private T result;
private Exception error;
public T getResult() {
return result;
}
public Exception getError() {
return error;
}
public AsyncTaskResult(T result) {
super();
this.result = result;
}
public AsyncTaskResult(Exception error) {
super();
this.error = error;
}
public void setError(Exception error) {
this.error = error;
}
}
and pass it to onPostExecute
return new AsyncTaskResult<String>(result)
or
return new AsyncTaskResult<String>(exception)
in onPostExecute you may check exception exists or not
asynctaskresult.getError() != null
You can use droidQuery to simplify everything and include HTTP error handling:
$.ajax(new AjaxOptions().url("http://www.example.com")
.type("POST")
.dataType("json")
.header("Accept", "application/json")
.header("Content-type", "application/json")
.timeout(1000)
.success(new Function() {
#Override
public void invoke($ d, Object... args) {
Toast.makeText(this, "SUCCESS!", Toast.LENGTH_SHORT).show();
JSONObject serverResponse = (JSONObject) args[0];
//handle response
}
})
.error(new Function() {
#Override
public void invoke($ d, Object... args) {
AjaxError error = (AjaxError) args[0];
//toast shows the error code and reason, such as "Error 404: Page not found"
Toast.makeText(this, "Error " + error.status + ": " + error.reason, Toast.LENGTH_SHORT).show();
}
}));

Android: Checking HTTP response code: getting 200; should be 302

I am checking a website for 302 messages, but I keep receiving 200 in my code:
private class Checker extends AsyncTask<Integer,Void,Integer>{
protected void onPreExecute(){
super.onPreExecute();
//display progressdialog.
}
protected Integer doInBackground(Integer ...code){
try {
URL u = new URL ( "http://www.reddit.com/r/notarealurlinredditqwerty");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("POST");
HttpURLConnection.setFollowRedirects(true);
huc.connect();
code[0] = huc.getResponseCode();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return code[0];
}
protected void onPostExecute(Integer result){
super.onPostExecute(result);
//dismiss progressdialog.
}
}
That is my Async task for checking. Here is the code implementing it:
int code = -1;
Checker checker = new Checker();
try {
code = checker.execute(code).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.d("code:", "" + code);
That log always returns 200, but I know that URL is 302 (it redirects to a search page on reddit.com).
What am I doing wrong?
This line just needs to be set to false:
HttpURLConnection.setFollowRedirects(false);
You can use HttpURLConnection. I have used it for response code in a few applications. I did not encounter any problems.
URL url = new URL("http://yoururl.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();

Categories

Resources