0

我有一个异步任务

public class AsynNetworkOperation extends AsyncTask<String,Void,Void>{

private Context context = null;
private ProgressDialog dialog = null;
private String title = "";
private WebRequest request = null;
private String accessMethod = "";
private HttpResponse response = null;
AsynResponse delegate = null;

public AsynNetworkOperation(Context context, String method, String dialogTitle)
{
    this.context = context;
    accessMethod = method;
    this.title = dialogTitle;
    delegate = (AsynResponse) context;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    dialog = new ProgressDialog(context);
    dialog.setMessage(title);
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
}

@Override
protected Void doInBackground(String... data) {
    // TODO Auto-generated method stub
    request = new WebRequest(context);
    if(accessMethod.equals(ServiceUri.AccessMethod.GET)){
        response = request.makeHttpGetCall(data[0]);
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    dialog.dismiss();
    delegate.responseResult(response);
    //dispose();
}

private void dispose()
{
    context = null;
    dialog = null;
    title = "";
    request = null;
    accessMethod = "";
    delegate = null;
}

}

和接口

public interface AsynResponse {

public void responseResult(HttpResponse response);

}

然后我有SqliteHelper

   //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());
}
}

和一MainActivity堂课

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
}
}

并获得 responsebody 代码是

    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();
}

它工作正常emulator,但它正在抛出network on main thread exceptiondevice我不知道它为什么抛出异常。还有其他network opeartion使用相同asyntask但在设备上工作正常。只有在这种情况下它是throwing exception。请帮我找出问题所在。

谢谢

4

2 回答 2

4

当它进入 getResponseBody 方法时,它在 while ((line = reader.readLine()) != null) 行崩溃

显然,您正在调用getResponseBody()主应用程序线程。在doInBackground()你的AsyncTask.

我不明白为什么在阅读内容时它会抛出异常。因为我已经有了 http 响应。

你不可以。您启动了 HTTP 请求。在关闭InputStreamHttpEntity. 那InputStream就是读取代表 HTTP 连接的套接字。

于 2014-04-08T11:58:16.793 回答
-2
add StrictMode:


if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                                                      }
于 2014-04-08T11:52:30.210 回答