Reputation: 1535
I develop a program that requires HttpUrlConnection. The problem is httpConn.connect() never success. I have checked if the network connection is available and status is connected before.
InputStream in = null;
int resCode = -1;
try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}*/
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
Adding this following code in onCreate method only prevent the program to force close.
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
I have added these permissions in manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Why it can happen and how to solve this problem?
Upvotes: 0
Views: 2220
Reputation: 513
There is mine :
public class YourClass extends AsyncTask<String, String, String> {
private AsyncResponse listener; //The listener interface
HttpURLConnection conn = null; //New object HttpURLConnection
public YourClass(AsyncResponse listener){
this.listener=listener;
//Params you need in this class
}
protected void onPreExecute() {
//Job you need to do before execute
}
@Override
protected String doInBackground(String... params) {
String response = "";
String responseError = "";
try{
//Connect to URL
Log.d("JSON", "Start of connexion");
URL url = new URL(params[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.connect();
int responseCode=conn.getResponseCode();
//HTTP_OK --> 200
//HTTP_CONFLICT --> 409
if (responseCode == HttpsURLConnection.HTTP_OK ) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
return response;
}
else if(responseCode == HttpURLConnection.HTTP_CONFLICT){
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getErrorStream()));
while ((line=br.readLine()) != null) {
responseError+=line;
}
return responseError;
}
else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null){
conn.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
try{
super.onPostExecute(result);
listener.onTaskCompleted(result);
}
catch(NullPointerException npe){
Log.d("NullPointerException", npe.getMessage());
}
}
}
AsynResponse is an interface, I implements this Interface to the class who call my AsyncTask, I override method OnTaskComplete. When the AsyncTask finish, this method is call.
More information : How to get the result of OnPostExecute()
How I use this class in my Activity who Implements AsynResponse interface :
new YourClass(this).execute("http://myUrl.com");
I hope this help.
Upvotes: 1