public interface AsynResponse {
public void responseResult(HttpResponse response);
}
and then I have an SqliteHelper class
//constructor
public SQLLiteDbHelper(Context context,int dbVersion) {
super(context,DATABASE_NAME, null, dbVersion);
this.context = context;
Log.d("tag","db version is "+DATABASE_VERSION);
crypt = new Cryptography();
utils = new Utils(context);
}
@Override
public void onCreate(final SQLiteDatabase db) {
String s;
try {
new AsynNetworkOperation(context, ServiceUri.AccessMethod.GET, "loading").execute(ServiceUri.SERVICE_URI+"s?d=abc");
} catch (Throwable t) {
Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
Log.d("tag",t.toString());
}
}
and a MainActivity class
public class MainActivity extends ListActivity implements AsynResponse{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchText = (EditText) findViewById(R.id.searchText);
utils = new Utils(MainActivity.this);
db=(new SQLLiteDbHelper(MainActivity.this,utils.getInt(Key.Db_version))).getReadableDatabase();
request = new WebRequest(this);
status = new AsynGetEmployeeStatus();
}
public void responseResult(HttpResponse response){
HttpEntity et = response.getEntity();
String strr = utils.getResponseBody(et); //it throw exception network on main thread
}
}
and get responsebody code is
public String getResponseBody(final HttpEntity entity) throws Exception {
InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
return null;
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
}
finally {
instream.close();
reader.close();
line=null;
}
return buffer.toString();
}
it is working fine on emulator and but it is throwing network on main thread exception on device.I dont know why it is throwing exception.There are other network opeartion also which use the same asyntask but that work fine on device. only in this case it is throwing exception. Please help me finding the problem.
thanks
解決
when it goes into getResponseBody method, it crashes at while ((line = reader.readLine()) != null) line
Apparently, you are calling getResponseBody() on the main application thread. Do that work in doInBackground() of your AsyncTask.
I am not getting why it is throwing exception while i am reading the content.because i have already the http response.
No, you do not. You started the HTTP request. You have not finished the HTTP request until you close the InputStream you get from the HttpEntity. That InputStream is reading off of the socket that represents the HTTP connection.
他のヒント
add StrictMode:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}