Create Post Method first
you need following jar files
1. commons-httpclient-3.0.1.jar
2. httpcore-4.2.1.jar
3. httpmime-4.2.1.jar
4. httpclient-4.2.jar
public class WebServiceUtil {
private static final String TAG = WebServiceUtil.class.getSimpleName();
public WebServiceUtil() {
}
public String postData(String URL, final String json) {
final DefaultHttpClient httpClient = createHttpClient();
final HttpPost httppost = new HttpPost(URL);
HttpResponse response = null;
try {
Log.d(TAG, "posted Url: " + URL + json.toString());
httppost.setHeader(HTTP.CONTENT_TYPE, "application/json");
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(new StringEntity(json));
response = httpClient.execute(httppost);
final String resp = EntityUtils.toString(response.getEntity(), "UTF-8");
Log.d(TAG,"Response : "+ resp.toString());
return resp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private DefaultHttpClient createHttpClient() {
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
final ClientConnectionManager cm = new ThreadSafeClientConnManager(
params, schemeRegistry);
final DefaultHttpClient client = new DefaultHttpClient(cm, params);
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
request.addHeader("User-Agent", "Mozilla/5.0");
}
});
return client;
}
}
********** Uploading Webservice ********
public class WsPhotoUpload {
private static final String TAG = WsPhotoUpload.class.getSimpleName();
private Context mContext;
public WsPhotoUpload(Context context) {
super();
this.mContext = context;
}
@SuppressWarnings("deprecation")
private String uploadImage(String url,String imgPath) {
// image path = sdcard image path i.e "/mnt/sdcard/test.jpg"
String response = "";
try {
HttpClient httpclient = new HttpClient();
final MultipartPostMethod mPost = new MultipartPostMethod(url);
httpclient.setConnectionTimeout(8000);
if (imgPath != null && !imgPath.equals("")) {
final File f1 = new File(imgPath);
if (f1.exists()) {
mPost.addParameter("filename", f1);
}
}
final JSONObject jsonObject = new JSONObject();
// parameters passed along with image.
jsonObject.put("created_at", currentDate);
mPost.addParameter("data", jsonObject.toString());
@SuppressWarnings("unused")
int statusCode1 = httpclient.executeMethod(mPost);
response = new String(mPost.getResponseBody());
Log.v(TAG, response);
mPost.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String executeService(final String url, final String imgPath) {
String response = "";
final String url = url;
response = uploadImage(url, imgPath);
return response;
}
}
you need following jar files
1. commons-httpclient-3.0.1.jar
2. httpcore-4.2.1.jar
3. httpmime-4.2.1.jar
4. httpclient-4.2.jar
public class WebServiceUtil {
private static final String TAG = WebServiceUtil.class.getSimpleName();
public WebServiceUtil() {
}
public String postData(String URL, final String json) {
final DefaultHttpClient httpClient = createHttpClient();
final HttpPost httppost = new HttpPost(URL);
HttpResponse response = null;
try {
Log.d(TAG, "posted Url: " + URL + json.toString());
httppost.setHeader(HTTP.CONTENT_TYPE, "application/json");
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(new StringEntity(json));
response = httpClient.execute(httppost);
final String resp = EntityUtils.toString(response.getEntity(), "UTF-8");
Log.d(TAG,"Response : "+ resp.toString());
return resp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private DefaultHttpClient createHttpClient() {
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
final ClientConnectionManager cm = new ThreadSafeClientConnManager(
params, schemeRegistry);
final DefaultHttpClient client = new DefaultHttpClient(cm, params);
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
request.addHeader("User-Agent", "Mozilla/5.0");
}
});
return client;
}
}
********** Uploading Webservice ********
public class WsPhotoUpload {
private static final String TAG = WsPhotoUpload.class.getSimpleName();
private Context mContext;
public WsPhotoUpload(Context context) {
super();
this.mContext = context;
}
@SuppressWarnings("deprecation")
private String uploadImage(String url,String imgPath) {
// image path = sdcard image path i.e "/mnt/sdcard/test.jpg"
String response = "";
try {
HttpClient httpclient = new HttpClient();
final MultipartPostMethod mPost = new MultipartPostMethod(url);
httpclient.setConnectionTimeout(8000);
if (imgPath != null && !imgPath.equals("")) {
final File f1 = new File(imgPath);
if (f1.exists()) {
mPost.addParameter("filename", f1);
}
}
final JSONObject jsonObject = new JSONObject();
// parameters passed along with image.
jsonObject.put("created_at", currentDate);
mPost.addParameter("data", jsonObject.toString());
@SuppressWarnings("unused")
int statusCode1 = httpclient.executeMethod(mPost);
response = new String(mPost.getResponseBody());
Log.v(TAG, response);
mPost.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String executeService(final String url, final String imgPath) {
String response = "";
final String url = url;
response = uploadImage(url, imgPath);
return response;
}
}
No comments:
Post a Comment