JSON Get and POST method using urlconnection
Get Method :
class GetData extends AsyncTask<String, Void, String> implements DialogInterface.OnCancelListener {
ProgressHUD mProgressHUD;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressHUD = ProgressHUD.show(PetGallery.this,"Getting Data", false,false, this);
}
@Override
protected String doInBackground(String... params) {
String msg = null; int status =0;
StringBuilder sb = new StringBuilder();
try {
URL get_url = new URL(Constant.GET_URL);
HttpURLConnection conn = (HttpURLConnection) get_url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
status = conn.getResponseCode();
msg = conn.getResponseMessage();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
sb.append(line);
break;
}
reader.close();
return sb.toString();
} catch (Exception e) {
Log.e("Http Response: ", msg);
}
return msg;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s!=null) {
String status;
try {
JSONObject jsonObject = new JSONObject(s);
status = jsonObject.getString(Constant.STATUS);
if (status.equals("success")) {
JSONArray jsonArray = jsonObject.getJSONArray(Constant.GET_ARRAY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString(Constant.PETID);
String url = object.getString(Constant.PHOTO_STRING);
String details = object.getString(Constant.DESC);
String place = object.getString(Constant.PLACE);
String date = object.getString(Constant.DATE);
String name = object.getString(Constant.NAME);
String contact = object.getString(Constant.CONTACT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(Constant.PETID, id);
map.put(Constant.PHOTO_STRING, url);
map.put(Constant.DESC, details);
map.put(Constant.PLACE, place);
map.put(Constant.DATE, date);
map.put(Constant.NAME, name);
map.put(Constant.CONTACT, contact);
//LoadImage.execute(map);
list.add(map);
}
lv.setAdapter(new CustomAdapter(PetGallery.this, list));
mProgressHUD.dismiss();
} else {
mProgressHUD.dismiss();
Toast.makeText(getApplicationContext(), "Data not found", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("Json Error", e.getMessage());
}
}
}
@Override
public void onCancel(DialogInterface dialog) {
this.cancel(true);
mProgressHUD.dismiss();
}
}
Post Method:
class GetTask extends AsyncTask<String, Void, String> implements DialogInterface.OnCancelListener{
ProgressHUD mProgressHUD;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressHUD = ProgressHUD.show(Pet.this,"Loading", false,false, this);
}
@Override
protected String doInBackground(String... params) {
URL url;
String response = "";
try {
SharedPreferences preferences = getSharedPreferences("Pet", 0);
String id = preferences.getString("id", null);
String data = URLEncoder.encode("pet_id", "UTF-8") + "=" + URLEncoder.encode(id, "UTF-8");
url = new URL("http://www.imstrong.info/ws/get_pet.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
os.close();
conn.connect();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response=String.valueOf(responseCode);
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
mProgressHUD.dismiss();
if(s!=null) {
try {
JSONObject object = new JSONObject(s);
simageurl = object.getString(Constant.PHOTO_STRING);
sdetails = object.getString(Constant.DESC);
splace = object.getString(Constant.PLACE);
sdate = object.getString(Constant.DATE);
sname = object.getString(Constant.NAME);
scontact = object.getString(Constant.CONTACT);
details.setText(sdetails);
place.setText(splace);
date.setText("(" + sdate + ")");
name.setText(sname);
contact.setText(scontact);
if(simageurl!=null){
Bitmap bitmap = getBitmapFromURL(simageurl);
photo.setImageBitmap(bitmap);
}else{
photo.setImageResource(R.drawable.logo);
}
}catch(Exception e){
Log.e("Json Responce : ", e.getMessage());
}
}
}
@Override
public void onCancel(DialogInterface dialog) {
this.cancel(true);
mProgressHUD.dismiss();
}
}
No comments:
Post a Comment