Why image receive slow from server json formate? - android

here i am receiving some picture from json in the form of json and display that picture in listview, but the problem is that images receive smoothly slow, mean display 1 pic and after3,4 second display one more and simillarly.
i want picture should display atonce,so what sholud i do? here is some code
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
JSONObject respObject;
try {
respObject = new JSONObject(s);
String active = respObject.getString("status_message");
if(active.equalsIgnoreCase("success")){
JSONArray array = respObject.getJSONArray("response");
for (int i =0; i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String icon= jsonObject.getString("image");
String name = jsonObject.getString("title");
String date = jsonObject.getString("date");
id = jsonObject.getString("id");
platformno = jsonObject.getString("platform");
//this is for integer datatype receving
// id = jsonObject.getInt("id");
//platformno = jsonObject.getInt("platform");
listitem.add(new Latest_list(icon,name,date,id,platformno));
}
lv.setAdapter(new Latest_customAdpater(LatestNews.this, listitem));
}else {
Toast.makeText(LatestNews.this, "services received Fail", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And here is custom adapter code
holder.icon=(ImageView)convertView.findViewById(R.id.NewsIconID);
holder.name=(TextView) convertView.findViewById(R.id.txt_newsNameID);
holder.date=(TextView) convertView.findViewById(R.id.txt_newsDateID);
holder.id= (TextView) convertView.findViewById(R.id.txt_hiddenID);
holder.platform=(TextView) convertView.findViewById(R.id.txt_hidden2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Latest_list services=newslist.get(position);
Picasso.with(context).load(services.getNews_icon()).into(holder.icon);
Log.d("Url",services.getNews_icon());
holder.name.setText(services.getNews_name());
holder.date.setText(services.getNews_date());
holder.id.setText(services.getNews_id());
holder.platform.setText(services.getNews_platform());

Related

Json move to next item

I have a dynamic JSON string that looks like this:
{"_id":"7","food_name":"Fiber Balance"},{"_id":"8","food_name":"Sport +"}
I am able to get the first name, but not the second one. This is my code for getting the first (Fiber Balance):
// Dynamic text
TextView textViewDynamicText = (TextView)getActivity().findViewById(R.id.textViewDynamicText);
String stringJSON = textViewDynamicText.getText().toString();
String stringFoodname = "";
try {
JSONObject jsonObject = new JSONObject(stringJSON);
Iterator<String> iter = jsonObject.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
stringFoodname = jsonObject.getString("food_name");
Toast.makeText(getContext(), stringFoodname, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// Something went wrong!
}
}
} catch (org.json.JSONException e) {
// Something went wrong!
}
How can I go to the next item in the json string?
If you have multiple data than you need to use Array,if you want to get all data from your json use below trick,
String json = "{\"_id\":\"7\",\"food_name\":\"Fiber Balance\"},{\"_id\":\"8\",\"food_name\":\"Sport +\"}";
json = "[" + json + "]";
try {
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String foodName = object.getString("food_name");
Log.e("FoodName:", foodName);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("error", "json", e);
}

posting data to server and get response. i want individual response of first name, email. please see below code once

#Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
System.out.println("my result:"+result);
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String fname = jsonObject.getString("First_Name");
String lname = jsonObject.getString("Last_Name");
String mail = jsonObject.getString("Email");
System.out.println("fname:"+fname+" lname:"+lname+" email:"+mail);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
In the above code,
posting data to server and get response. i want individual response of first name, email. can you please rectify my mistake.
You should parse as JSONObject, not as JsonArray as per your response from the server.
#Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
System.out.println("my result:"+result);
try {
JSONObject jsonObject = new JSONObject(result);
String fname = jsonObject.optString("First_Name");
String lname = jsonObject.optString("Last_Name");
String mail = jsonObject.optString("Email");
System.out.println("fname:"+fname+" lname:"+lname+" email:"+mail);
}
catch (Exception e) {
e.printStackTrace();
}
}

Regd : getting value from json url

I want to get the id ^& content value in http://rest-service.guides.spring.io/greeting
What i tried is,
try {
JSONObject jsonObj = new JSONObject(parsingUrl);
// If you have array
JSONArray resultArray = jsonObj.getJSONArray("id"); // Here you will get the Array
// Iterate the loop
for (int i = 0; i < resultArray.length(); i++) {
// get value with the NODE key
JSONObject obj = resultArray.getJSONObject(i);
String name = obj.getString("content");
}
// If you have object
//String result1 = jsonObj.getString("result");
} catch (Exception e) {
e.printStackTrace();
}
Thanks
The url that your mentioned dont have json arrays, parsing will be like
try {
JSONObject jsonObj = new JSONObject(resultfromUrl);
int id = jsonObj.getInt("id");
String name = jsonObj.getString("content");
} catch (JSONException e) {
e.printStackTrace();
}

Covnert JSON to string in android

I am trying to get the user details using people.get in which I have emails which I need to get that value.
This is what I mean:
This is how I'm trying to do but unable to get the value
try {
String resp =profile.getEmails().toString();
JSONObject mainObject = new JSONObject(resp);
JSONObject uniObject = mainObject.getJSONObject("emails");
String email = uniObject.getString("value");
((TextView) findViewById(R.id.txtemail)).setText(email);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help is appreciated.
In your JSON response, emails is a JSONArray, not a JSONObject. You would need to get it like this:
JSONObject json= new JSONObject(responseString); //your response
try {
JSONArray responseArray = jsonObj.getJSONArray("email");
for (int i = 0; i < responseArray.length(); i++) {
// get value with the NODE key
JSONObject obj = responseArray.getJSONObject(i);
String lastName = obj.getString("value");
String firstName = obj.getString("type");
EmailResponse myResp = new EmailResponse();
myResp.setValue(value);
myResp.setType(type);
//set all other Strings
//lastly add this object to ArrayList<MyResponse> So you can access all data after saving
}
}
catch (JSONException e) {
e.printStackTrace();
}
POJO Class:
public class EmailResponse{
public String value = "";
public String type = "";
//getter setters
}
Hope this helps.

nested AsyncTask and onPostExecute

I am using a AsyncTask (1) in another AsyncTask (2).
AsyncTask 1 fetches online user data, counts the number of entries in the response and for each entry, in onPostExecute displays a username and runs a new AsyncTask (2) to fetch a image from a server and load it into a ImageView. This all happens in onPostExecute.
This is working flawlessly, the user data is fetched and shown, and the images are shown one by one for each entry.
However, the itteration through the array and the updating of the TextView in AsyncTask 1's onPostExecute happens so fast, it basically only shows the last user name in the array, the other ones are loaded, but impossible to detect with the human eye :)
Meanwhile, AsyncTask 2 is still fetching images from online, and showing profile images for the wrong users.
The problem I have here obviously, is these 2 need to be in sync.
So I thought I just wait for the output in AsyncTask 2 with the get() method, but now nothing is updated at all anymore, no TextView...this is unexpected behaviour for me.
So, the question is how to sync the 2 AsyncTasks?
bit of code to clarify, if it's still needed
//instantiate first AsyncTask
new AsyncRequest().execute(bundle);
private class AsyncRequest extends AsyncTask<Bundle, Void, String> {
protected String doInBackground(Bundle... bundle) {
String data = null;
try {
data = request(null, bundle[0]); //request the data
return data;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}// end method
protected void onPostExecute(String response) {
JSONArray data = null;
try {
JSONObject response2 = Util.parseJson(response);
data = response2.optJSONArray("data");
int amount = data.length();
TextView s1 = (TextView) findViewById(R.id.some_id);
s1.setText("" + amount); //displays number of items
//display the data
for(int i=0; i<amount; i++){
String email = "";
String id = "";
JSONObject json_obj = data.getJSONObject(i);
Log.d("JSONObject ", ""+json_obj);
String name = json_obj.getString("name");
if (json_obj.has("email")){
email = json_obj.getString("email");
}
if (json_obj.has("id")){
id = json_obj.getString("id");
}
String picture = "http://www.domain.com/"+id+"/picture";
TextView s2 = (TextView) findViewById(R.id.name_placeholder);
s2.setText(name);
//here we do a new AsynTask for each entry and wait until the data is fetched
new DownloadProfileImageTask().execute(picture, name).get();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}// end method
It is not quite clear why you are calling setText of the single TextView with the name of multiple names. As you have mentioned, although you had setText of all the names, you only see a single name. May be you need to use a ListView or something like that.
Now regarding your question: probably you don't need two AsyncTasks. You can do everything in a single AsyncTask. The code will be something like below:
//Create a Holder class as a data holder.
//For simplicity, public attributes are used
class Holder{
public String name;
public String email;
public String id;
public BitmapDrawable imageDrawable;
}
//instantiate the AsyncTask
new AsyncRequest().execute(bundle);
private class AsyncRequest extends AsyncTask<Bundle, Holder, Integer> {
protected Integer doInBackground(Bundle... bundle) {
int amount = 0;
try {
data = request(null, bundle[0]); //request the data
JSONArray data = null;
JSONObject response2 = Util.parseJson(response);
data = response2.optJSONArray("data");
amount = data.length();
//display the data
for(int i=0; i<amount; i++){
Holder holder = new Holder();
holder.email = "";
holder.id = "";
JSONObject json_obj = data.getJSONObject(i);
Log.d("JSONObject ", ""+json_obj);
holder.name = json_obj.getString("name");
if (json_obj.has("email")){
holder.email = json_obj.getString("email");
}
if (json_obj.has("id")){
holder.id = json_obj.getString("id");
}
String picture = "http://www.domain.com/"+id+"/picture";
//Fetch the image and create a Drawable from it - Synchronously
holder.imageDrawable = getImageDrawable(picture, name);
publishProgress(holder);
}
} catch (Exception e) {
e.printStackTrace();
}
return amount;
}// end method
protected void onProgressUpdate(Holder... holder) {
//Update the user name and image
TextView s2 = (TextView) findViewById(R.id.name_placeholder);
s2.setText(holder[0].name);
ImageView imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageDrawable(holder[0].imageDrawable);
}
protected void onPostExecute(Integer amount) {
TextView s1 = (TextView) findViewById(R.id.some_id);
s1.setText(amount.toString()); //displays number of items
}// end method

Categories

Resources