Retrieve Data From Server
Hello friends, I created web serviceAPI and that i am exploitation http://192.168.43.157:8080/StudentApp/StudentServlet, link in my android app for taking information.
Visit First Tutorial for Create WebService API .
click here How to Connect Android App with Java Server?
My previous article made-to-order ListView with Image and Text provides you an honest summary of customizing a listing read that contains a fingernail image and few text fields. All the list information are downloaded by creating a network calls. the most challenge in this tutorial is downloading the pictures asynchronously and caching them. attributable to lack smart|of excellent|of fine} tools I used a 3rd party library (it could be a good library though) to transfer the listview information and caching the pictures.
Today i'm about to make a case for an equivalent, however now with a distinct approach by victimisation AN another library known as mechanical man Asynchronous protocol consumer . By victimisation humanoid Asynchronous protocol consumer you'll be able to see good improvement in listview performance while not a lot of effort.
Add Library in android side >>http://loopj.com/android-async-http/
YouTube Channel :
MainActivity: put below code in your MainActivity.java
MainActivity
package com.example.aaru.jsonapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Array;
import java.util.ArrayList;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<StudentBeam>allist;
AsyncHttpClient asyncHttpClient;
String My_URL="http://192.168.43.157:8080/StudentApp/StudentServlet";
StudentBeam beam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allist=new ArrayList<>();
getData();
Toast.makeText(MainActivity.this, "onSuccess", Toast.LENGTH_SHORT).show();
listView= (ListView) findViewById(R.id.listView);
}
private void getData() {
//add asynhttp client lib.....
asyncHttpClient=new AsyncHttpClient();
asyncHttpClient.get(My_URL,new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
for (int i=0;i<response.length(); i++){
try {
Toast.makeText(MainActivity.this, "onSuccess", Toast.LENGTH_SHORT).show();
JSONObject object=response.getJSONObject(i);
beam=new StudentBeam(object.get("name").toString(),object.get("address").toString(),object.get("phone").toString());
//also check json object key
Toast.makeText(MainActivity.this, "onSuccess"+beam.getName().toString(), Toast.LENGTH_SHORT).show();
allist.add(beam);
CustomAdapter adapter=new CustomAdapter(MainActivity.this,allist);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Toast.makeText(MainActivity.this, "onFailed", Toast.LENGTH_SHORT).show();
}
});
}
}
StudentBeam : Create Java class.Put below data (The Student beam is Add multiple data in array list).
StudentBeam
package com.example.aaru.jsonapp;
/**
* Created by Aaru on 10/9/2017.
*/
public class StudentBeam {
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
String Name;
String Address;
String Phone;
public StudentBeam(String Name,String Address,String Phone){
this.Name=Name;
this.Address=Address;
this.Phone=Phone;
}
}
CustomAdapter : Create Java Class name is CustomAdapter and extend with BaseAdapter.
CustomAdapter
package com.example.aaru.jsonapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Aaru on 10/9/2017.
*/
public class CustomAdapter extends BaseAdapter {
ArrayList<StudentBeam> list;
Context context;
LayoutInflater inflater;
public CustomAdapter(Context context,ArrayList<StudentBeam> list){
this.list=list;
this.context=context;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=new ViewHolder();
if (convertView==null) {
convertView = inflater.inflate(R.layout.raw, null);
holder.name= (TextView) convertView.findViewById(R.id.txtName);
holder.Address= (TextView) convertView.findViewById(R.id.txtAddress);
holder.Phone= (TextView) convertView.findViewById(R.id.txtPhone);
convertView.setTag(holder);
holder.name.setText(list.get(position).getName());
holder.Address.setText(list.get(position).getAddress());
holder.Phone.setText(list.get(position).getPhone());
}else {
holder= (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ViewHolder{
TextView name;
TextView Address;
TextView Phone;
}
}
>>>>>>>>>>>>>>>>>>>Run App and Enjoy<<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>Also Comment below<<<<<<<<<<<<<<<<<<<<<
Comments
Post a Comment
Please Comment if Any Query !!