2016-12-20 24 views
1

私はAsyncTaskクラスを使用してデータベースに接続しています。データに基づいて、私は動的ファイルEditTextCheckboxを作成し、XMLファイルは使用しません。doInBackgroundメソッドでUIまたはUIスレッドを呼び出す方法

lView = new LinearLayout(this);- エラーが発生しました。

enter image description here

doInBackground方法の内のUIスレッドをコールする方法はあります!

ありがとうございます!

@Override 
protected String doInBackground(String... param) { 

    HashMap<String, bean> map = new HashMap<String, bean>(); 

    try { 
     url = new URL("http://localhost/app/alldata.php"); 
    } catch (MalformedURLException e) { 
     Toast.makeText(getApplicationContext(), "URL Exception", Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
     return null; 
    } 

    try { 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(READ_TIMEOUT); 
     conn.setConnectTimeout(CONNECTION_TIMEOUT); 
     conn.setRequestMethod("POST"); 

     // setDoInput and setDoOutput method depict handling of both send and receive 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 

     // Append parameters to URL 
     Uri.Builder builder = new Uri.Builder() 
       .appendQueryParameter("user_id", "user_id") 
       .appendQueryParameter("dpt_id","dptid"); 
     String query = builder.build().getEncodedQuery(); 

     // Open connection for sending data 
     OutputStream os = conn.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
     writer.write(query); 
     writer.flush(); 
     writer.close(); 
     os.close(); 
     conn.connect(); 

    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

    try { 
     int response_code = conn.getResponseCode(); 
     lView = new LinearLayout(this); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } finally { 
     conn.disconnect(); 
    } 

    return null; 
} 
+0

本当にバックグラウンドで新しいLinearLayoutを作成する必要がありますか? 'onPostExecute'で作成することができます – JuLes

答えて

3

あなたはdoInBackground方法からpublishProgress()を呼び出し、UIスレッドで呼び出されますonProgressUpdateを上書きすることができます。

明らかに、これは進歩的な使用のために意図されています。バックグラウンドの作業単位を明確に分けて、通常はonPostExecuteで処理する必要があります。

1

あなたは本当にあなたが使用することができ、メインUIスレッドと通信したい場合:

runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
     //place your code here 
    } 
}); 
0

あなたAsyncTaskはその仕事を終えたときに、それが呼び出すことができるようにこれを行うための、よりエレガントな方法は、コールバック関数を渡していますメソッド呼び出しはActivityにあり、そこで必要な変更を加えることができます。

このようなインターフェイスを維持することをお勧めします。あなたもそのAsyncTaskにリスナーを渡す必要があり、あなたのAsyncTaskを実行する際に

public interface HttpResponseListener { 
    void httpResponseReceiver(String result); 
} 

は今、あなたのActivityに、あなたはこのリスナーを実装する必要があります。今で

public YourActivity extends Activity implements HttpResponseListener { 

    // ... Other functions 

    @Override 
    public void httpResponseReceiver(String result) { 
     int response_code = (int) Integer.parseInt(result); 

     // Take necessary actions here 
     lView = new LinearLayout(this); 
    } 
} 

AsyncTaskあなたActivityにリスナーとしての最初の変数を持っている必要があります。あなたのActivity、今から

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

    // Declare the listener here 
    public HttpResponseListener mHttpResponseListener; 

    @Override 
    protected String doInBackground(String... param) { 

     HashMap<String, bean> map = new HashMap<String, bean>(); 

     try { 
      url = new URL("http://localhost/app/alldata.php"); 
     } catch (MalformedURLException e) { 
      Toast.makeText(getApplicationContext(), "URL Exception", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
      return null; 
     } 

     try { 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(READ_TIMEOUT); 
      conn.setConnectTimeout(CONNECTION_TIMEOUT); 
      conn.setRequestMethod("POST"); 

      // setDoInput and setDoOutput method depict handling of both send and receive 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      // Append parameters to URL 
      Uri.Builder builder = new Uri.Builder() 
        .appendQueryParameter("user_id", "user_id") 
        .appendQueryParameter("dpt_id","dptid"); 
      String query = builder.build().getEncodedQuery(); 

      // Open connection for sending data 
      OutputStream os = conn.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
      writer.write(query); 
      writer.flush(); 
      writer.close(); 
      os.close(); 
      conn.connect(); 

     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 

     try { 
      // int response_code = conn.getResponseCode(); // Return the result to onPostExecute 
      // lView = new LinearLayout(this); // Remove this from here 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      conn.disconnect(); 
     } 

     return conn.getResponseCode() + ""; 
    } 

    // Set the result here 
    @Override 
    protected void onPostExecute(final String result) { 
     mHttpResponseListener.httpResponseReceiver(result); 
    } 
} 

あなたが開始しているAsyncTaskあなたは、このように最初のリスナーを割り当てる必要があります。

希望します。

関連する問題