Get the library from http://loopj.com/android-async-http/
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
void getProducts(){
// Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params;
params = new RequestParams();
params.put("code", "101245");
client.post(constants.PRODUCTS_URL,
params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String response = new String(responseBody);
System.out.println(response);
// prgDialog.hide();
try {
JSONObject arr = new JSONObject(response);
System.out.println(arr.getInt("success"));
if (arr.getInt("success") == 1) {
products = arr.getJSONArray("products");
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String name = c.getString("title");
String path = "http://skydent.in/app-admin/"+c.getString("image");
int pid = c.getInt("pid");
Constants.productList.add(new HomeGridViewModel(name,"", path,pid));
}
checkLoginStatus();
} else {
Toast.makeText(getApplicationContext(),
"No Slider Found!", Toast.LENGTH_LONG)
.show();
finish();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(
getApplicationContext(),
"Error Occured [Server's JSON response might be invalid]!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
finish();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
if (statusCode == 404) {
Toast.makeText(getApplicationContext(),
"Requested resource not found",
Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getApplicationContext(),
"Something went wrong at server end",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
getApplicationContext(),
"Please connect to Internet and try again!",
Toast.LENGTH_LONG).show();
}
finish();
}
});
}
No comments:
Post a Comment