Httppost Giving Error In Api 23 Android
I have a method in my android app which uses HttpPost class. It was working fine with targeted sdk 4.4.2 but i've made some changes and made the targeted sdk to 23(6.0). Now HttpPo
Solution 1:
Replace your code inside function with below given code, using HttpUrlConnection
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class TestJava {
private String getJSON(String URL, JSONObject obj) throws JSONException {
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(URL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(30000);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.write(obj.toString().getBytes("UTF-8"));
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
Post a Comment for "Httppost Giving Error In Api 23 Android"