You have to create some classes to get json response
for example your json url is like :- http://67.222.29.105/demo/apps/index.php/operation/active_events
create class name JsonHttpPost
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class JsonHttpPost {
String result = null;
private static final int TIMEOUT_MILLISEC = 20000;
private static final String serverurl = "http://67.222.29.105/demo/apps/";
HttpParams params = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(params);
HttpResponse response;
HttpEntity entity;
HttpPost post;
public String post(String operation,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
System.out.println("Server Link: "+serverurl+operation);
post = new HttpPost(serverurl+operation);
if(nvp != null)
{
post.setEntity(new UrlEncodedFormEntity(nvp));
}
response = client.execute(post);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
Log.d("Json Response ","******"+result);
return result;
}
public String deleteImage(String string)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
post = new HttpPost(string);
response = client.execute(post);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String deleteVideo(String string)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
post = new HttpPost(string);
response = client.execute(post);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadImage(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("addressbook_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("addressbook_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("addressbook_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadImageAddEvent(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
// System.out.println("Server Link: "+serverurl+op);
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//System.out.println("Upload Image: "+nvp);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("before_event_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("before_event_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("before_event_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadAttendeeImage(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
// System.out.println("Server Link: "+serverurl+op);
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//System.out.println("Upload Image: "+nvp);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("attendee_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("attendee_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("attendee_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadUserImage(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
// System.out.println("Server Link: "+serverurl+op);
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//System.out.println("Upload Image: "+nvp);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("user_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String post(String request)
{
String result = null;
try
{
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(params);
//System.out.println("Getting url: "+request);
System.out.println("Server Link: "+serverurl+request);
HttpPost post = new HttpPost(serverurl+request);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
}
create new class name GetOperationName
public class GetOperationName {
final String operation = "operation";
String link;
public String getOPName(String OPERATION_NAME) {
// TODO Auto-generated method stub
link = "/"+operation+"/"+OPERATION_NAME;
return link;
}
public String getOPNameQue(String OPERATION_NAME) {
// TODO Auto-generated method stub
link = "/"+operation+"/"+OPERATION_NAME + "?";
return link;
}
public String getField(String FIELD_NAME) {
return "&"+FIELD_NAME;
}
public String getTwoField(String string, String string2) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2;
}
public String getFourField(String string, String string2, String string3,
String string4) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3+"&"+string4;
}
public String getFiveField(String string, String string2, String string3,
String string4, String string5) {
// TODO Auto-generated method stub
string5 = string5.replaceAll(" ","%20");
//System.out.println("5th string: "+string5.replaceAll(" ","%20"));
//System.out.println("Result: "+"&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5);
return "&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5;
}
public String getThreeField(String string, String string2, String string3) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3;
}
public String getEightField(String string, String string2, String string3,
String string4, String string5, String string6, String string7,
String string8) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5+"&"+string6+"&"+string7+"&"+string8;
}
public String getTenField(String string, String string2, String string3,
String string4, String string5, String string6, String string7,
String string8, String string9, String string10) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5+"&"+string6+"&"+string7+"&"+string8+"&"+string9+"&"+string10;
}
}
create new class name GetNameValue
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
public class GetNameValue {
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
public List<NameValuePair> getEventPastDetails(String struserid) {
// TODO Auto-generated method stub
nvp.add(new BasicNameValuePair("user_id",struserid));
//nvp.add(new BasicNameValuePair("E_ID",struserid));
return nvp;
}
}
create class name Settings
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class Settings {
public static String OPERATION_NAME = null;
public static final String TRUE = "True";
public static final String NO_NETWORK = "Can not connect to internet";
public static final CharSequence LOADING = "Loading";
public static final String NO_RESPONSE = "Not getting data";
public static final String LOGIN_PAGE = "index.php";
public static final String SIGNUP_PAGE = "index.php";
public static boolean isNetworkAvailable(Context context)
{
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return info!=null;
}
}
In your main Activity on create function
this is start code for getting json response.
if(!TextUtils.isEmpty(user_id)){
boolean chkNet = Settings .isNetworkAvailable(context);
if (chkNet == true) {
ActiveEventProcess activeevent = new ActiveEventProcess();
activeevent.execute(this);
}
}
put below code out side oncreate method
declare ProgressDialog gloably above oncreate method
ProgressDialog prodialog;
@SuppressWarnings("rawtypes")
public class ActiveEventProcess extends AsyncTask {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
prodialog = new ProgressDialog(context);
prodialog.setMessage("Loading");
prodialog.show();
prodialog.setCancelable(false);
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
String result;
Log.i(tag, "my user id is.." + user_id);
try {
JsonHttpPost httpPost = new JsonHttpPost();
GetOperationName chngpassop = new GetOperationName();
GetNameValue nv = new GetNameValue();
Settings .OPERATION_NAME = "active_events";
String signupop = chngpassop
.getOPNameQue(TixeSettings.OPERATION_NAME);
List<NameValuePair> nvp = nv.getEventPastDetails(user_id);
result = httpPost
.post(Settings .SIGNUP_PAGE + signupop, nvp);
//If you want to upload image from json url ,and if you get image in this json response and you want to get it then use below code.
// result = httpPost.uploadImage(TixeSettings.SIGNUP_PAGE + //signupop, nvp);
Log.i(tag, "result" + result);
// here in result you got the json response
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prodialog.dismiss();
// Do your stuff here
}
}
for example your json url is like :- http://67.222.29.105/demo/apps/index.php/operation/active_events
create class name JsonHttpPost
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class JsonHttpPost {
String result = null;
private static final int TIMEOUT_MILLISEC = 20000;
private static final String serverurl = "http://67.222.29.105/demo/apps/";
HttpParams params = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(params);
HttpResponse response;
HttpEntity entity;
HttpPost post;
public String post(String operation,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
System.out.println("Server Link: "+serverurl+operation);
post = new HttpPost(serverurl+operation);
if(nvp != null)
{
post.setEntity(new UrlEncodedFormEntity(nvp));
}
response = client.execute(post);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
Log.d("Json Response ","******"+result);
return result;
}
public String deleteImage(String string)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
post = new HttpPost(string);
response = client.execute(post);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String deleteVideo(String string)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
post = new HttpPost(string);
response = client.execute(post);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadImage(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("addressbook_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("addressbook_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("addressbook_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadImageAddEvent(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
// System.out.println("Server Link: "+serverurl+op);
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//System.out.println("Upload Image: "+nvp);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("before_event_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("before_event_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("before_event_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadAttendeeImage(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
// System.out.println("Server Link: "+serverurl+op);
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//System.out.println("Upload Image: "+nvp);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("attendee_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("attendee_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("attendee_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String uploadUserImage(String op,List<NameValuePair> nvp)
{
try
{
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpContext localContext = new BasicHttpContext();
// System.out.println("Server Link: "+serverurl+op);
post = new HttpPost(serverurl+op);
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//System.out.println("Upload Image: "+nvp);
if(nvp != null)
{
for(int index=0; index < nvp.size(); index++)
{
if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
{
if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
{
// If the key equals to "U_PHOTO", we use FileBody to transfer the data
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
else
{
if(nvp.get(index).getName().equalsIgnoreCase("user_photo"+index))
{
//System.out.println("---In if UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
}
else
{
// Normal string data
//System.out.println("---In else UMI_NAME---");
multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
}
}
}
}
post.setEntity(multipartEntity);
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public String post(String request)
{
String result = null;
try
{
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(params);
//System.out.println("Getting url: "+request);
System.out.println("Server Link: "+serverurl+request);
HttpPost post = new HttpPost(serverurl+request);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if(entity != null)
{
result = EntityUtils.toString(entity);
}
else
{
Log.d("Response Failed","Response from server is failed");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
}
create new class name GetOperationName
public class GetOperationName {
final String operation = "operation";
String link;
public String getOPName(String OPERATION_NAME) {
// TODO Auto-generated method stub
link = "/"+operation+"/"+OPERATION_NAME;
return link;
}
public String getOPNameQue(String OPERATION_NAME) {
// TODO Auto-generated method stub
link = "/"+operation+"/"+OPERATION_NAME + "?";
return link;
}
public String getField(String FIELD_NAME) {
return "&"+FIELD_NAME;
}
public String getTwoField(String string, String string2) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2;
}
public String getFourField(String string, String string2, String string3,
String string4) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3+"&"+string4;
}
public String getFiveField(String string, String string2, String string3,
String string4, String string5) {
// TODO Auto-generated method stub
string5 = string5.replaceAll(" ","%20");
//System.out.println("5th string: "+string5.replaceAll(" ","%20"));
//System.out.println("Result: "+"&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5);
return "&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5;
}
public String getThreeField(String string, String string2, String string3) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3;
}
public String getEightField(String string, String string2, String string3,
String string4, String string5, String string6, String string7,
String string8) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5+"&"+string6+"&"+string7+"&"+string8;
}
public String getTenField(String string, String string2, String string3,
String string4, String string5, String string6, String string7,
String string8, String string9, String string10) {
// TODO Auto-generated method stub
return "&"+string+"&"+string2+"&"+string3+"&"+string4+"&"+string5+"&"+string6+"&"+string7+"&"+string8+"&"+string9+"&"+string10;
}
}
create new class name GetNameValue
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
public class GetNameValue {
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
public List<NameValuePair> getEventPastDetails(String struserid) {
// TODO Auto-generated method stub
nvp.add(new BasicNameValuePair("user_id",struserid));
//nvp.add(new BasicNameValuePair("E_ID",struserid));
return nvp;
}
}
create class name Settings
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class Settings {
public static String OPERATION_NAME = null;
public static final String TRUE = "True";
public static final String NO_NETWORK = "Can not connect to internet";
public static final CharSequence LOADING = "Loading";
public static final String NO_RESPONSE = "Not getting data";
public static final String LOGIN_PAGE = "index.php";
public static final String SIGNUP_PAGE = "index.php";
public static boolean isNetworkAvailable(Context context)
{
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return info!=null;
}
}
In your main Activity on create function
this is start code for getting json response.
if(!TextUtils.isEmpty(user_id)){
boolean chkNet = Settings .isNetworkAvailable(context);
if (chkNet == true) {
ActiveEventProcess activeevent = new ActiveEventProcess();
activeevent.execute(this);
}
}
put below code out side oncreate method
declare ProgressDialog gloably above oncreate method
ProgressDialog prodialog;
@SuppressWarnings("rawtypes")
public class ActiveEventProcess extends AsyncTask {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
prodialog = new ProgressDialog(context);
prodialog.setMessage("Loading");
prodialog.show();
prodialog.setCancelable(false);
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
String result;
Log.i(tag, "my user id is.." + user_id);
try {
JsonHttpPost httpPost = new JsonHttpPost();
GetOperationName chngpassop = new GetOperationName();
GetNameValue nv = new GetNameValue();
Settings .OPERATION_NAME = "active_events";
String signupop = chngpassop
.getOPNameQue(TixeSettings.OPERATION_NAME);
List<NameValuePair> nvp = nv.getEventPastDetails(user_id);
result = httpPost
.post(Settings .SIGNUP_PAGE + signupop, nvp);
//If you want to upload image from json url ,and if you get image in this json response and you want to get it then use below code.
// result = httpPost.uploadImage(TixeSettings.SIGNUP_PAGE + //signupop, nvp);
Log.i(tag, "result" + result);
// here in result you got the json response
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prodialog.dismiss();
// Do your stuff here
}
}
No comments:
Post a Comment
Comments