Hebrew Date web request android - android

I am trying to get Hebrew date in my Android app. I could not find an API that I could use.
Edit: How can I integrate the API in to my codes?
final TextView mTextView = (TextView) findViewById(R.id.text);
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.hebcal.com/shabbat/?cfg=json&geonameid=3448439&m=50";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
mTextView.setText("Response is: " + response.substring(0,500));
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
queue.add(stringRequest);
UPDATE: Instead of calling hebcal.com, I used http://kosherjava.com/ and downloaded their API into my project. It is providing the Hebrew date for me wihtout any issues.

From what I've seen the link that you're providing seems ok.
You should get started by checking the GSON library and the annotations that you can use to create POJOs out of JSON (http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html).
Basically, you'll replace the handling in onResponse with a clever way to obtain the hebrew date out the JSON.
Let me know if you need more details on how to implement it.

Related

How to send a list of integers as a request body in android using volley?

My Restful API expect a list of integer arrays. The API works fine when I use postman client to send an json array like so [1,3,4].
I am using android volley to make requests. I have a list of integers which I want to send to the API like this.
//converting integer array to a json array
JSONArray myIntegerJsonArray = new JSONArray(myIntegerArray);
String url = "myurl...";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, myIntegerJsonArray, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//... do stuff here
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
MySingletonRequestQueue.getInstance(this).addToRequestQueue(request);
I am currently getting HttpMessageNotReadableException: Required request body is missing error. So I am guessing it's not sending the myIntegerJsonArray for some reason. What am I doing wrong?

Parse url with curly brackets in Android App

How can i Parse url with curly brackets like
http://example.com/api/login/{username}/{password} in an android application.
Normal volley post request returns html.But i need JSON.
Integrating Login API in Android App
If I get you right, what you want to do is send a GET request for login.
The following code can help you (it is not recommended to use GET for login, use POST instead. I'm giving a GET ex because that's what your'e asking for):
final TextView mTextView = (TextView) findViewById(R.id.text);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://example.com/api/login/your_username/your_password";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});

How to save values of JSON String iinto Android internal SQLite database using Volley?

I am new to Volley and liking it so far because it seems simple to make a HTTP call even if you are in the main thread / OnCreate class.
So far I am having success retrieving the JSON String from the endpoint and can display the first few characters in my TextBox. Now I want to save it to my internal NoSQL DB in my android app. My problem is I am clueless on how to directly save this JSON String into my android app's SQLite DB. Does Volley have a direct method for this? My Table only has 2 fields and will only need to fetch the two key-value pairs in the JSON.
Below is what I have working so far.
I am not even sure if the part DownloadManager.Request.NETWORK_WIFI is the right thing to do but so far it is working.
btn_update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String url = "http://10.0.2.2/api/wordsupdate.php";
// Request a string response from the provided URL.
//StringRequest stringRequest = new StringRequest(DownloadManager.Request.Method.GET, url,
StringRequest stringRequest = new StringRequest(DownloadManager.Request.NETWORK_WIFI, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first characters of the response string.
txt_output.setText("Response is: " + response.substring(0, 100));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
txt_output.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
//queue.add(stringRequest);
queue.add(stringRequest);
}
});
Below is the JSON from the endpoint
[{"id":"1","param1":"word1","param2":"word1Equivalent","version":"1"},{"id":"2","param1":"word2","param2":"word2Equivalent","version":"1"},{"id":"3","param1":"word3","param2":"word3Equivalent","version":"1"}]
You can convert the JSONObject into String and save. While retrieving the same column convert the String into JSONObject.
Below is the example,
String stringToBeSaved = jsonObject.toString(); (can be save to DB as text or varchar)
Hope it helps!

Volley Request Inside a Service

What i want is to request a server using volley inside a service continuously, When a new entry is added in a database it notify a user that a new entry is available. I used volley to request a server and when a new request is added in a database it notify in a notification bar.
First you should add it to your build.gradle file in your project.
dependencies {
...
compile 'com.android.volley:volley:1.0.0'}
To make a request using Volley you should create a RequestQueue and pass it Request objects. The RequestQueue will manage the threads for the network, parsing, reading and writing operations. Below a quick code I did to make a request using the Volley lib.
private void consultarObjJson() {
RequestQueue pilha = Volley.newRequestQueue(this);
String url=webserviceCaminho+"alunos";
StringRequest consultaString = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String resposta) {
Log.d(TAG, "Resposta" + resposta);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<AlunoObj> post = new ArrayList<AlunoObj>();
post = Arrays.asList(mGson.fromJson(resposta, AlunoObj[].class));
alunoAdapter = new AlunoAdapter(getBaseContext(), post, "MainActivity");
recyclerView.setAdapter(alunoAdapter);
alunoAdapter.notifyDataSetChanged();
progressBar.setProgress(100);
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError erro) {
Log.d(TAG, "Erro :" + erro.getMessage());
}
}); pilha.add(consultaString);
}
For more details about how to use it check here Volley - Android developers

Android Volley no Response from StringRequest

(Sorry for any english mistake its not my native language)
I'm trying to parse html using Volley and Jsoup.
while debugging / running the app, the StringRequest dosent invoke onResponse() or onErrorResponse(), basically there no response or Error from the Response.Listener (i guess).
so i cant "begin" parsing the html because the onResponse() is never invoked.
Ive searched for answer and nothing seems to get it fixed.
Could it be that the RequestQueue cache kicks in and its not trying to get the response? (just brain storming tried so many things).
--Im just trying to retrive data from html, so i could show the updated data from the url in my app, is Volly the way or should i use simpler mathods?
here is my volley StringRequest and RequestQueue:
String url = "http://www.fringeb7.co.il/";
final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
response_utf8 = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"),"UTF-8");
doc = Jsoup.parse(response_utf8);
Log.d("logr=",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("log2=", error.toString());
requestQueue.stop();
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
took me long enough, but there was no problem.
the code works, while debugging there were no response,
but when i run the app the request complete and i got the wanted response.
so if anyone have a similier problem, from some reason while debugging I dident get response but while running the app its working fine.
(simpley dident know that)
try this first before wasting time in debugging like i did.
If you use Jsoup, you can get it and parse in very simple way:
Document doc = Jsoup.connect("http://www.fringeb7.co.il/").get();
If you still want to use Volley, below code works perfectly in my device:
String url = "http://www.fringeb7.co.il/";
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
String response_utf8 = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"), "UTF-8");
Document doc = Jsoup.parse(response_utf8);
Log.d("logr=", "title = " + doc.title());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("log2=", error.toString());
//requestQueue.stop();
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
requestQueue.start();
Output of the above code:
D/logr=: title = תיאטרון הפרינג' באר שבע
try 'https' instead of 'http' in your url

Categories

Resources