HttpUrlConnection POST request not working. Tell me if there is any other way to make POST request in android.Tell me if there is any other way to make POST request in android.Tell me if there is any other way to make POST request in android.
public final String apiCall(String pUrl) {
if( ! isInternetAvailable() )
return "NO_INTERNET";
try {
URL lUrl = new URL(pUrl.replace(" ", "%20"));
Log.i("url", String.valueOf(lUrl));
String url = pUrl;
Log.i("dom", url.substring(0, (url.indexOf('?') - 1)));
Log.i("para", url.substring((url.indexOf('?') + 1), url.length()) );
URL obj = new URL(url.substring(0,(url.indexOf('?')-1)));
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "GYUserAgentAndroid");
con.setRequestProperty("Content-Type", "application/json");
String urlParameters = url.substring((url.indexOf('?')+1), url.length());
Log.i("urlParameters", urlParameters.toString());
// Send post request
con.setDoInput(true); // true if we want to read server's response
con.setDoOutput(true); // false indicates this is a GET request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Log.i("res",response.toString());
return response.toString();
}catch (Exception e) {
Log.i("secondEx",e.toString());
return "ERROR";
}
}