I'm trying to get response with user json list from github api.
Could anyone tell me the reason why code execution path doesn't reach overriden methods onFailure() and onSuccess?
public String getResponse()
{
AsyncHttpClient client=new AsyncHttpClient();
RequestParams params=new RequestParams();
params.put("since","0");
client.get("https://api.github.com/users", params, new AsyncHttpResponseHandler() {
#Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
userList.clear();
userList.addFirst("Items didn't load properly");
}
#Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
try {
content = new String(arg2, "UTF-8");
//content is json response that can be parsed.
} catch (UnsupportedEncodingException e1) {
userList.clear();
userList.add("Some encoding problems occured");
e1.printStackTrace();
}
}
});
return content;
}
These methods just ignored for some reason. After client.get(...) it jumps right to return content.
Any ideas about the reason of it? What am I doing wrong?
Would appreciate any advice.
EDIT:
SO the proper way is to do that is to operate with response within the onSuccess(...) method?
#Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
try {
content = new String(arg2, "UTF-8");
//content is json response that can be parsed.
parseResponseAmount(content, 10); //operate with response
} catch (UnsupportedEncodingException e1) {
userList.clear();
userList.add("Some encoding problems occured");
e1.printStackTrace();
}
}
And I try to parse information from response like:
private void parseResponseAmount (String response, int amount)
{
try {
JSONArray readerArray = new JSONArray(response);
for (int i = 0; i < amount; i++)
{
JSONObject userObject = (JSONObject) readerArray.get(i);
String login = userObject.getString("login");
getUserList().add(login);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
getUserList().clear();
getUserList().add("Failed to parse response");
e.printStackTrace();
}
}
but that code still doesn't work. In event-driven development it caused by mouse move, key presses etc. What causes these onSuccess() and onFailure() to occur? What is the precondition?
You're using AsyncHttpClient. Do you know what "Async" means?
It is a shortening of "Asynchronous", which means "occurring independently of the main program flow". Your main program thread will not wait for the AsyncHttpClient to complete its request before continuing. That's why your getResponse() method return immediately.
You should design your app to operate on event-driven programming principles; this means, in your case, that you would spawn whatever process or method you need to handle the response from the onSuccess() interface method.
So, you probably won't be returning anything from getResponse(). You can make it void. The AsyncHttpResponseHandler() you're instantiating and passing to the get() method is an interface which the library will call when certain events take place—that is, when the GET request succeeds or fails.
Finally, you will have to show your data somehow. I'm assuming it is in a ListView? If so, you will need to notify the ListView that its data has changed (or possibly recreate the adapter with new data). See this question. Also, I'm not sure but you may have to use runOnUIThread() to update your views, since this is running on a different thread.
Related
I'm new to Android, I'm using AsyncHttpClient to call a POST API. But the API is not even being called
Below is my code:
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Key","random-key");
JSONObject body = new JSONObject();
body.put("clientId","random-client-id");
body.put("question",question);
HttpEntity entity = new StringEntity(body.toString());
client.post( getApplicationContext(),"http://localhost:3000/api/Chats/GetAnswer", entity,"application/json", new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
List<List<Answer>> answers = new ArrayList<>();
try {
JSONArray answersJson = response.getJSONArray("answers");
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable error) {
Toast toast = Toast.makeText(getApplicationContext(),"Unable to get answers for the question sent",Toast.LENGTH_SHORT);
toast.show();
}
});
`
Any hints of what I'm doing wrong??
Solved, it appear that the problem was in AndroidManifest.xml Since I'm using the internet and calling an external API, I had to add:
<uses-permission android:name="android.permission.INTERNET"/>
And another thing. When working locally and testing using the emulator, we should write
http://10.0.2.2:port-number/ instead of http://localhost:port-number/. Because android emulator runs in a virtual machine. Therefore, localhost will be emulator's own loopback address.
Please put debugger at your method and Make sure its calling or not , I think you have passed wrong context value And as my point of View its better to User Retrofit than AsyncHttpClient.
Use Retrofit if you are communicating with a Web service.
I want to send parameters such as username and password.
I got an error like String cannot be converted to jsonobject.
I dont know what this happening.Anyone pls help me my code is:
JSONObject obj=new JSONObject();
try{
obj.put("username","test");
obj.put("password","test");
} catch (JSONException e) {
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
urlJsonObj, obj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq,json_obj_req);
}
There is nothing wrong with the way you are creating JSONObject and putting values in it. Make sure the response received is Json, because your onResponse method accepts JSONObject. You could be receiving String value as response, which could not be converted to JSONObject.
It looks like your response is actually a string and not a json object i.e. {"object":"value"} but rather "object:value". You need to sniff your response via either Stetho, Fiddler or reenact your request via Postman (or Fiddler)
======================
This doesn't answer your question, but this will help you tremendously and make your life easier.
Highly recommend using Gson and Retrofit to make HTTP requests and parse Gson objects easily.
https://github.com/google/gson
http://square.github.io/retrofit/
i am fetching 104 variables from MySQL through php in android and trying to show in list view but its taking almost 1 - 2 minutes to load values how to speed up the data fetching.
i am using json for fetching data, any other library's for fast fetching.
please explain with complete example.
Try the Android Asynchronous Http Client library
Features
Using upstream HttpClient of version 4.3.6 instead of Android provided DefaultHttpClient
Compatible with Android API 23 and higher
Make asynchronous HTTP requests, handle responses in anonymous callbacks
HTTP requests happen outside the UI thread
Requests use a threadpool to cap concurrent resource usage
GET/POST params builder (RequestParams)
Multipart file uploads with no additional third party libraries
Streamed JSON uploads with no additional libraries
Handling circular and relative redirects
Tiny size overhead to your application, only 90kb for everything
Automatic smart request retries optimized for spotty mobile connections
Automatic gzip response decoding support for super-fast requests
Binary protocol communication with BinaryHttpResponseHandler
Built-in response parsing into JSON with JsonHttpResponseHandler
Saving response directly into file with FileAsyncHttpResponseHandler
Persistent cookie store, saves cookies into your app’s SharedPreferences
Integration with Jackson JSON, Gson or other JSON (de)serializing libraries with BaseJsonHttpResponseHandler
Support for SAX parser with SaxAsyncHttpResponseHandler
Support for languages and content encodings, not just UTF-8
For more details please visit these link :
http://loopj.com/android-async-http/
Here's an example of using it :
pdialog.setMessage("Veuillez patienter!");
pdialog.show();
RequestParams params = new RequestParams();
params.put("user_login", username);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://yourUrl.com", params, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
loading = true;
AdapterPrincipal adapterPrincipal;
String s = "";
try {
s = new String(responseBody, "UTF-8");
Log.d("Error", s.toString());
JSONObject arrg = new JSONObject(s);
JSONArray query = arrg.getJSONArray("query");
Log.i("result from query ", "" + query);
for (int i = 0; i < query.length(); i++) {
try {
pdialog.dismiss();
JSONObject object = query.getJSONObject(i);
String dateinsertannonce = object.getString("date_insert_annonce");
Log.i("date", dateinsertannonce);
String datevente = object.getString("vendu");
String marque = object.getString("marque");
String Clomn_Model = object.getString("model");
String Clomn_Prix = object.getString("prix");
String Clomn_Kilometrage = object.getString("kilometrage");
String Clomn_BoiteVitesse = object.getString("boite_vitesse");
String Clomn_Energie = object.getString("energie");
String Clomn_Source = object.getString("source");
String Clomn_Url = object.getString("url");
String Clomn_PHOTO = object.getString("images_noms");
String Maj = object.getString("derniere_maj");
int id = object.getInt("id");
voitureList = databaseHelper.getAllVoiture(username, currentLength);
listView.setAdapter(new AdapterLogin(getActivity(), voitureList, username, currentLength));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
});
}
This is basically my end url which is giving me correct address component in the browser. But when I am using volley to fetch the same data I am getting error 403.
final StringBuilder url = new StringBuilder(
"http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=");
url.append(latitude);
url.append(',');
url.append(longitude);
url.append("&language=");
url.append(Locale.getDefault().getLanguage());
// Request a string response from the provided URL.
#SuppressWarnings("rawtypes")
StringRequest stringRequest = new StringRequest(Request.Method.GET, url.toString(),
new Response.Listener() {
#Override
public void onResponse(Object arg0) {
// TODO Auto-generated method stub
System.out.println();
try {
String address = (new JSONObject(arg0.toString())).getJSONArray("results").getJSONObject(0).getString("formatted_address");
location.setText(address);
ProjectUtil.crossfade(layout, progress);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println();
}
});
queue.add(stringRequest);
With the API request in the REST client gives the response that is expected out of it. In these cases it is always the network that cause trouble, Please check to see the network you are using for your android app to make connection is a shared network? And/or one that uses a proxy for outgoing
requests? Looks like someone on the same network is
downloading stuff from Google and causing blocks.
One reason may be also because you are exceeding the API request limit. For more details read about the quota request.
For more information, I would like to see the logcat!!
I use an Asynchronous Http Client for Android for loading my JSON data.(https://github.com/h-r/android-async-http-with-caching)
It works perfect, but i want to cache json files if user has no connection.
I know how to check if user had internet connection etc, but i use a version of Asynchronous Http Client of loopj who has caching enabled for httpresponse's
The problem is that my code give's just a error..
WhtsnxtBase.get(dateje, null, new JsonHttpResponseHandler() {//get url with date of json file
#Override
public void onSuccess(JSONObject timeline) {
// code removed isn't relevant for this question
}
#Override
public void onFailure(Throwable e) {Log.e("MyActivity", "OnFailure!", e);}
#Override
public void onFailure(Throwable e, String response) {Log.e("MyActivity", "OnFailure!", e);}
#Override
public void onFailure(Throwable e, JSONArray errorResponse) {Log.e("MyActivity", "OnFailure!", e);}
});
This is the url (to see wat kind of date it is etc..http://calvijn.tk/mobiel/2013-07-19)
The cache don't work do anybody know how to fix it, or how to use the cached library properly?
If it isnt't possible this way, does anybody know a good opensource cache manager or something to cache the JSON files the right way.
I would recommend parsing these JSON responses into a local java object:
https://code.google.com/p/google-gson/
I use GSON to serialize the JSON objects into a Java Class. This makes it easier to handle the data within your app.
If you want to persist data properly when the user is offline, you may want to look into using Android's SQLite database for persisting a large array of objects. For something simple, you can use ORMLite.
Right now I'm using ORMLite and built out a custom content provider to handle local data. This allows me to use the SimpleCursorAdapter and LoaderManagers.
Inside your JsonHttpResponseHandler you get your JSON in onSuccess() so you need to cache data from there, while you have two options : store in sqlite or use file caching, onfailure() is called when you don't get your object ex no connection or timeout.
public class JSONCache
{
public static void writeToCache( String fileName ,JSONObject jObject )
{
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(fileName)));
out.writeObject( jObject );
out.close();
}
public static JSONObject readFromCache(String fileName )
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(fileName)));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
retrun jsonObject;
}
}
and in your response handler
WhtsnxtBase.get(dateje, null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(JSONObject timeline)
{
...
JSONCache.writeToCache( "cache_file_path" , timeline );
//updateUI(timeline);
}
#Override
public void onFailure(Throwable e)
{
Log.e("MyActivity", "OnFailure!", e);
JSONCache.readFromCache( "cache_file_path" );
}
#Override
public void onFailure(Throwable e, String response)
{
Log.e("MyActivity", "OnFailure!", e);
JSONObject timeline = JSONCache.readFromCache( "cache_file_path" );
//updateUI(timeline);
}
#Override
public void onFailure(Throwable e, JSONArray errorResponse)
{
Log.e("MyActivity", "OnFailure!", e);
JSONObject timeline = JSONCache.readFromCache( "cache_file_path" );
//updateUI(timeline);
}
});