/* here mention your package name for example: com.karthik.sekar*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
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.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
JSONObject JSONresponseText, result;
int timeout1 = 10000*2;
int timeout2 = 10000*2;
// constructor
Context context;
public JSONParser() {
}
public JSONObject getJSONFromUrl(JSONObject data, String requesturl,
String pOST_METHOD) {
// Making HTTP request
String result = "";
Log.d("requesturl = ", requesturl);
Log.d("pOST_METHOD = ", pOST_METHOD);
try {
// defaultHttpClient
URL url = new URL(requesturl);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(20000);
connection.setRequestMethod(pOST_METHOD);
connection.setRequestProperty("content-Type", "application/json");
connection.setRequestProperty("content-Language", "en-US");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dom = new DataOutputStream(
connection.getOutputStream());
dom.writeBytes(data.toString());
dom.flush();
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
dom.close();
reader.close();
Log.d("SERVICE RESPONSE: ", answer.toString());
result = answer.toString();
try {
JSONresponseText = new JSONObject(result);
Log.i("JSONresponseText", JSONresponseText.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
Log.d("JSON RESPONSE = ", JSONresponseText.toString());
return JSONresponseText;
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
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.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
JSONObject JSONresponseText, result;
int timeout1 = 10000*2;
int timeout2 = 10000*2;
// constructor
Context context;
public JSONParser() {
}
public JSONObject getJSONFromUrl(JSONObject data, String requesturl,
String pOST_METHOD) {
// Making HTTP request
String result = "";
Log.d("requesturl = ", requesturl);
Log.d("pOST_METHOD = ", pOST_METHOD);
try {
// defaultHttpClient
URL url = new URL(requesturl);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(20000);
connection.setRequestMethod(pOST_METHOD);
connection.setRequestProperty("content-Type", "application/json");
connection.setRequestProperty("content-Language", "en-US");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dom = new DataOutputStream(
connection.getOutputStream());
dom.writeBytes(data.toString());
dom.flush();
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
dom.close();
reader.close();
Log.d("SERVICE RESPONSE: ", answer.toString());
result = answer.toString();
try {
JSONresponseText = new JSONObject(result);
Log.i("JSONresponseText", JSONresponseText.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
Log.d("JSON RESPONSE = ", JSONresponseText.toString());
return JSONresponseText;
}
}
No comments:
Post a Comment