Im trying to download data from a server in the form of a JSON object, parse that object then use the data elsewhere in my app.
Ive created a class which downloads the data (confirmed with a Log.v statement)
The trouble is that i want to display the results in a RecyclerView and the List of objects that i generate after parsing the JSON response does not get generated until after the adapter method is called from my main class.
So my question is, given the code below, how can i ensure that the on response method only exits once the parseResponse method has finished. Currently i am returning the (Null) _releaseList from my requestAndPareseReleaseList method.
public class ParseReleaseJSON extends JSONObject {
String _url;
List<ReleaseInfo> _releaseList = Collections.emptyList();
ParseReleaseJSON(String url) {
super();
_url = url;
}
public List<ReleaseInfo> requestAndParseReleaseList(Context _context){
JsonObjectRequest jsObReq = new JsonObjectRequest(Request.Method.GET, _url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
releaseList = parseResponse(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.v("err", "nosuccess");
}
});
MySingleton.getInstance(_context).addToRequestQueue(jsObReq);
return _releaseList;
}
private List parseResponse(JSONObject response) {
List<ReleaseInfo> list = new ArrayList<>();
if(response == null || response.length() == 0){
return list;
}
try {
if(response.has("results")){
JSONArray resultsArray = response.getJSONArray("results");
for(int i = 0, j = 6; i < j; i++){
ReleaseInfo release = new ReleaseInfo();
JSONObject tempObj = resultsArray.getJSONObject(i);
release.title = tempObj.getString("title");
release.date = tempObj.getString("date");
list.add(release);
}
return list;
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
}
Related
I am trying to call another async task inside an OnPostExecute. The 2nd task does not run at all it seems. I am unable to print anything from within to the logs.
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject json = new JSONObject(result);
JSONArray lignes = json.getJSONArray("lignes");
populatelist(lignes);
}
catch (JSONException e) {
}
}
}
The populatelist function fills an array. Inside this function, I try to call the 2nd async task to get values based on this list.
protected void populatelist(JSONArray lignes){
try {
for(int i=0;i<lignes.length(); i++) {
JSONObject jsonas = lignes.getJSONObject(i);
String fdesignation = jsonas.getString("designation");
String fqtecde = jsonas.getString("qtecde");
String fcode_produit = jsonas.getString("code");
InfoStock(fcode_produit);
items.add(new PickingListProduitItem(fdesignation,"",fqtecde, ""));
}
}
catch(Exception e){
}
}
InfoStock() is the function that is used to return additional from a web service.
protected void InfoStock(String code_produit){
String stockURL = "http://" + mSharedPreferences.getString(Constants.SERVER_IP,"")+"//rest/v2/produit/info/code/"+ code_produit + "?stock=true";
try {
if (mDownloader != null && mDownloader.getStatus() == AsyncTask.Status.RUNNING) {
mDownloader.cancel(true);
mPDialog.dismiss();
}
mPDialog = new ProgressDialog(getApplicationContext());
mDownloader = new XMLDownloader(getApplicationContext(),mPDialog);
byte[][] downloadResults = mDownloader.execute(stockURL).get();
// Read stock info.
String s = new String(downloadResults[0], StandardCharsets.UTF_8);
JSONObject resp = new JSONObject(s);
PrixStockJSONParser psj = new PrixStockJSONParser(resp);
mRepInfoStock = psj.getRepInfoStock();
mRepInfoPrix = psj.getRepInfoPrix();
} catch (Exception ex) {
}
}
I am trying to set a value in the array <> created in the OnPostExecute Method. However there is no error message and the array is not populated at all. Even if I try to print a log from the InfoStock function, it does nothing.
Any suggestions are welcome.
I am trying to parse the response I get from my OkHttp3Connection and it always almost always returns me String with value "true" which is names[0] in this example.
Now I try to convert this string to boolean and return true/false depending on the response. Now I guess since the OkHttp3Connection takes some time it doesnt let the query to finish and returns false which is the default value.
What I was trying to say is that this function always returns false no matter what String names[0] is.
public boolean checkIfSongPurhcased(String songName){
final boolean[] songCheckResult = new boolean[1];
OkHttp3Connection.doOkHttp3Connection("", Services_Url_class.GET_SONG_PURCHASED, OkHttp3Connection.Request_type.POST, request_data, new OkHttp3Connection.OkHttp3RequestCallback() {
#Override
public void onSuccess(String result, String userTag) throws JSONException {
JSONObject jsonResult = new JSONObject(result);
JSONArray data = jsonResult.getJSONArray("data");
if(data != null) {
String[] names = new String[data.length()];
for(int i = 0 ; i < data.length() ; i++) {
JSONArray arr = data.getJSONArray(i);
JSONObject dataObj = arr.getJSONObject(0);
Iterator<String> keys = dataObj.keys();
names[i] = dataObj.getString(keys.next());
}
//names[0] is "true"
songCheckResult[0] = Boolean.parseBoolean(names[0]);
}
}
#Override
public void onError(String error, String userTag) {
System.out.println("songy" + error);
}
}
);
//always returns false
return songCheckResult[0];
How can I make it so if names[0] is "true" it would give me true.
You should pass the listener/callback.
First create callback interface like below
public interface OnQueryResultCallback{
void onQueryResult(String songName, boolean status);
}
Then pass listener/callback to your method, here is the modified method.
public void checkIfSongPurhcased(final String songName, final OnQueryResultCallback listener){
OkHttp3Connection.doOkHttp3Connection("", Services_Url_class.GET_SONG_PURCHASED, OkHttp3Connection.Request_type.POST, request_data, new OkHttp3Connection.OkHttp3RequestCallback() {
#Override
public void onSuccess(String result, String userTag) throws JSONException {
JSONObject jsonResult = new JSONObject(result);
JSONArray data = jsonResult.getJSONArray("data");
if(data != null) {
String[] names = new String[data.length()];
for(int i = 0 ; i < data.length() ; i++) {
JSONArray arr = data.getJSONArray(i);
JSONObject dataObj = arr.getJSONObject(0);
Iterator<String> keys = dataObj.keys();
names[i] = dataObj.getString(keys.next());
}
//names[0] is "true"
boolean status = Boolean.parseBoolean(names[0]);
listener.onQueryResult(songName,status);
}
}
#Override
public void onError(String error, String userTag) {
System.out.println("songy" + error);
listener.onQueryResult(songName,false);
}
}
);
}
Then call checkIfSongPurhcased like below
checkIfSongPurhcased("songName", new OnQueryResultCallback(){
public void onQueryResult(String songName, boolean status){
if(status){
// do something on success
}else{
// do something on failure
}
}
});
Try to use getBoolean() rather than getString().
For Eg: names[i] = dataObj.getBoolean(keys.next());
I was using this in another place as async but I wanted to refactor to make it reusable, how can I reorganize the code in order to work as a consumable class?. It doesn't work if it's not async and the ip of the backend is well defined so it's not that. Any ideas?
public class HTTPRequestManager {
public static JSONArray fetchData(){
return null;
}
public static String postData(Context context, String url, String JSONData) {
return null;
}
/* #Override
protected Integer doInBackground(String... strings) {
try {
//1.create client Object
OkHttpClient client = new OkHttpClient();
//2.Define request being sent to server
RequestBody postData = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSONData);
Request request = new Request.Builder()
.url(context.getResources().getString(R.string.backend_base_url) + url)
.post(postData)
.build();
//3.Transport the request and wait for response to process next
Response response = client.newCall(request).execute();
String resultData = response.body().string();
if (resultData.equals("OK")) {
} else {
//post failed
return "FAILED";
}
return resultData;
} catch (Exception e) {
Log.d("API_CONNECTION_ERROR", "Couldn't connect to the API");
return "API_CONNECTION_ERROR";
}
}*/
}
I used to have this annonymous class embeeded in another class and it works(it's a get request) but the problem is that it's not reusable in that way:
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
URL url = null;
#Override
protected void onPreExecute() {
getActivity().setProgressBarIndeterminateVisibility(true);
}
#Override
protected Integer doInBackground(String... params) {
Integer result = 0;
HttpURLConnection urlConnection;
try {
url = new URL (getResources().getString(R.string.backend_base_url) +
"api/flrcks/user/id/0/latitude/3000/longitude/300/within/9999999999999999999999999");
urlConnection = (HttpURLConnection) url.openConnection();
int statusCode = urlConnection.getResponseCode();
// 200 represents HTTP OK
if (statusCode == 200) {
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
response.append(line);
}
parseResult(response.toString());
result = 1; // Successful
} else {
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}
#Override
protected void onPostExecute(Integer result) {
// Download complete. Let us update UI
progressBar.setVisibility(View.GONE);
if (result == 1) {
adapter = new MyRecyclerAdapter_Nearby(getActivity(), feedsList);
mRecyclerView.setAdapter(adapter);
checkAdapterIsEmpty();
} else {
Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_SHORT).show();
t.setVisibility(View.VISIBLE);
}
}
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.getJSONArray("rows");
feedsList = new ArrayList<>();
JSONArray members;
for (int i = 0; i < posts.length(); i++) {
memberList = new ArrayList<>();
final JSONObject post = posts.optJSONObject(i);
members=post.getJSONArray("members");
final FeedItem item = new FeedItem();
//for (int i = 0; i < posts.length(); i++) {
//JSONObject post = posts.optJSONObject(i);
//FeedItem item = new FeedItem();
item.setId(post.optString("id"));
item.setTitle(post.optString("name"));
item.setDescription(post.optString("description"));
item.setPrivacy(post.optString("privacy_mode_description"));
item.setInitial_date(post.optString("initial_date"));
item.setThumbnail(post.optString("thumbnail"));
item.setColor_hex(post.optString("color_hex"));
item.setTag(post.optString("tag"));
item.setDistance(post.optInt("st_distance"));
//item.setThumbnail(post.optString("thumbnail"));
for(int k=0; k <members.length();k++)
{
MemberItem memberItem = new MemberItem();
JSONObject member = members.optJSONObject(k);
memberItem.setName(member.optString("name"));
memberItem.setUsername(member.optString("username"));
memberItem.setProfile_pic(member.optString("profile_pic"));
memberItem.setIs_moderator(member.optBoolean("is_moderator"));
memberItem.setFacebookId(member.optString("facebook_id"));
memberList.add(memberItem);
}
item.setMemberList(memberList);
feedsList.add(item);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
What changes do I need to make to put it in an isolated file to be consumed by the whole app like for example in a file called OkHTTPRequests.class???
Create an interface
public interface OnWebResponseListener {
void onWebResponse(CommonUtilities.services service, String result);
}
create a public enum for identifying service. in my case i created a CommonUtilities java where i declared
public enum services {
LOGIN
}
Your Common File
public class CallAddr extends AsyncTask<String, Void, String> {
CommonUtilities.services service;
OnWebResponseListener onWebResponseListener;
String url;
FormBody.Builder body;
Request request;
OkHttpClient client;
final static String TAG = "CallAddr";
public CallAddr(Map<String, String> data, CommonUtilities.services service, OnWebResponseListener onWebResponseListener, String url) {
this.service = service;
this.onWebResponseListener = onWebResponseListener;
this.url = url;
body = new FormBody.Builder();
for (String key : data.keySet()) {
body.add(key, data.get(key));
}
client = new OkHttpClient();
}
#Override
protected String doInBackground(String... strings) {
String result = "";
request = new Request.Builder().url(url).post(body.build()).build();
try {
Response response = client.newCall(request).execute();
result = response.body().string();
} catch (Exception e) {
Log.e(TAG,Log.getStackTraceString(e));
}
return result;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (onWebResponseListener != null) {
onWebResponseListener.onWebResponse(service, s);
}
}
}
I am having some problem when trying to execute different method in servlet doGet(). So when my button on click, it will pass along the eventID:
viewDtlEventBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent eventDtlIntent = new Intent(context, EventDetail.class);
eventDtlIntent.putExtra("eventID", eventIDTV.getText());
startActivity(eventDtlIntent);
}
});
Then in my EventDetail class, I am executing the method in AsyncTask class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_detail);
context = this;
Bundle extras = getIntent().getExtras();
if (extras != null) {
eventID = extras.getString("eventID");
}
eventModel.setEventID(eventID);
new GetEventDetailAsyncTask(context).execute(eventModel);
}
And in my AsyncTask class, I am calling the method in my controller which retrieving the JSON returned from servlet:
#Override
protected Double doInBackground(Event... params) {
try {
eventCtrl.getEventDetailByID(params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
And in my controller class:
public Event getEventDetailByID(Event event) throws JSONException {
Event eventModel = new Event();
String page;
JSONArray jsonArray;
String eventID = event.getEventID();
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(ENeighbourhoodActivity.URL
+ "?getEventDetailByID&eventID=" + eventID +"");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
page = "{\'EventDetail\':" + responseString + "}";
try {
JSONObject jsonObject = new JSONObject(page);
jsonArray = jsonObject.getJSONArray("EventDetail");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
JSONObject attribute = jsonArray.getJSONObject(i);
String eventName = attribute.getString("eventName");
eventModel.setEventName(eventName);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return event;
}
And basically from here, I am accessing the servlet. And in my doGet() in serlvet class:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
JSONArray jsonArray = new JSONArray();
PrintWriter out = response.getWriter();
if (request.getParameter("getAllEvent") != null) {
}
catch (JSONException je) {
System.out.println(je.getMessage());
} catch (Exception exc) {
System.out.println(exc.getMessage());
}
out.println(jsonArray.toString());
}
}
I already have one if statement for some other methods. I wonder how should I pass the getEventDetailByID and the eventID parameter into servlet so that it knows which method to runs.
Thanks in advance.
Here is a example of how you can get the eventID value.
//gets value from getEventDetailByID parameter.
String detail = request.getParameter("getEventDetailByID");
if (detail != null && !detail.equals("")) {
int eventId = Integer.parseInt(request.getParameter("eventID"));
//get event detail by id with id
//e.g resultObject = myMethod(detail, eventId);
}
Update 1:
A better way of doing what is required:
String action = request.getParameter("action");
if (action.equalsIgnoreCase("GetById")) {
int eventId = Integer.parseInt(request.getParameter("eventID"));
//get event detail by id with id
//e.g resultObject = getById(eventId);
} else if (action.equalsIgnoreCase("GetAllEvents")) {
//Get all events
//e.g resultObject = GetAllEvents();
} else {
}
The URL usage:
to get event by id:
http://localhost:8080/WebService/EventDetailServlet?action=GetById&eventID=46
to get all event details
http://localhost:8080/WebService/EventDetailServlet?action=GetAllEvents
So the title says it all. I'm getting a compilation error inside of my JsonObjectRequest
I'm sure this is a really simple fix, but I'm not exactly sure how to write it. I'm still in the early stages of android development, so pardon my noob-ness. :)
Here's the code.
public class Api {
public static final String SEARCH_URL = "http://api.worldoftanks.ru/2.0/account/list/?application_id=171745d21f7f98fd8878771da1000a31&search=";
private RequestQueue mQueue;
private ArrayList<String> mNames;
public Api(Context context) {
mQueue = Volley.newRequestQueue(context);
}
public ArrayList<String> searchPlayer(String name) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, SEARCH_URL + name, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject json) {
try {
JSONArray data = json.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String n = name.getString("nickname");
mNames.add(n);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.getMessage());
}
}
);
mQueue.add(jsonObjectRequest);
return mNames;
}
}
As the error says, replace
public ArrayList<String> searchPlayer(String name)
with
public ArrayList<String> searchPlayer(final String name)
The argument must be declared final to ensure that it never gets changed by the code inside the method, and thus can be safely used inside the response listener.
Add final to the argument declaration:
public ArrayList<String> searchPlayer(final String name)