I am new to android development I want parse the JSON Data, I want get response from server by posting this below JSON data
// login JSON
mtjson={
"action":"login",
"data" :{
"email":"ajit@globemoving.net",
"password":"ajit@globemoving.net",
"mobile":"9845031333"
}
}
// retrieve enquiry list JSON
mtjson={
"action":"eq-list",
"data" :{
"vid":"108"
}
}
// retrieve enquiry details JSON
mtjson={
"action":"eq-details",
"data" :{
"vid":"108", // vendor ID
"eqid":"eq72873", // enquiry ID
"state":"new" // state = new,quoted
}
}
// submit new bid for enquiry JSON
mtjson={
"action":"bid",
"data" :{
"vid":"108", // vendor ID
"eqid":"eq72873", // enquiry ID
"bid_amount":"5000",
"mobile": "9876543623",
"note":"dkjhdah"
}
}
Hence please help with code with Httpurlconnection method. coz i am using new android studio apche.org is deprecated.
MainActivity
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler webreq = new ServiceHandler();
HashMap<String, String> params = new HashMap<>();
JSONObject j1=new JSONObject();
JSONObject j2=new JSONObject();
try { j2.put("vid", "108");
String e = j2.toString();
j1.put("action", "eq-list");
j1.put("data", e); }
catch (JSONException e) { e.printStackTrace(); }
// String params = j1.toString();
// Log.d("output", String.valueOf(j2));
// Making a request to url and getting response
String jsonStr = webreq.makeWebServiceCall(url, ServiceHandler.POST, params);
Log.d("Response: ", "> " + jsonStr);
studentList = ParseJSON(jsonStr);
return null;`
Service Handler
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
//Constructor with no parameter
public ServiceHandler() {
}
/**
* Making web service call
*
* @url - url to make request
* @requestmethod - http request method
*/
public String makeWebServiceCall(String url, int requestmethod) {
return this.makeWebServiceCall(url, requestmethod, null);
}
/**
* Making service call
*
* @url - url to make request
* @requestmethod - http request method
* @params - http request params
*/
public String makeWebServiceCall(String urladdress, int requestmethod,
String params) {
URL url;
String response = "";
try {
url = new URL(urladdress);
DataInputStream input;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
if (requestmethod == POST) {
conn.setRequestMethod("POST");
} else if (requestmethod == GET) {
conn.setRequestMethod("GET");
}
if (params != null) {
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
writer.write(result.toString());
writer.flush();
writer.close();
os.close();
}
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 = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}