Blog : application not working on Android 4.1

application not working on Android 4.1


Hi all I have sample applications which works on other platforms (from 1.6+) But same application is not working on my new Android 4.1 simulator. Problem is regarding HttpConnection. Means suppose I want to do login then I am not able to do that. There is no problem in code, because it works fine on all other AVD(1.6,2.1,2.2).

I also tried with simple httpconnection code but still that is not working.

There is any special setting requires to run internet on simulator for 4.1. But on my previous platforms I had not done any setting.

My HttpConnection code is as:

public String httpGetResponse(String url) {
  connectionUrl = url;
  query_string="";
  String response = null;
  try {
  int loc = url.indexOf('?');
  if(loc>-1){
  try {
  query_string=url.substring(loc);
  } catch (Exception e) {
  query_string="";
  }
  }
  processGetRequest();
  HttpResponse httpresponse = httpclient.execute(host, get);
  response = EntityUtils.toString(httpresponse.getEntity());
  } catch (Exception e) {
  response = null;
  }
  return response;
}
I am calling this method as:

response = connectionUtil.httpGetResponse("My URL");
On all other platforms I got response as String , But with 4.1 I am getting response as:

response:
There are two Solution of this Problem.

1) Don't write network call in Main UI Thread, Use Async Task for that.

2) Write below code into your MainActivity file after setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);
}
And below import statement into your java file.

import android.os.StrictMode;