2012-04-27 4 views
1

私は初めてandynctaskを使ってアンドロイド用のアプリを作成するのは初めてです。私はバックグラウンドでhttpostを実行したいが、エラーが発生し続ける。私は正しいパラメータを使用していますか? postexecuteも必要ですか?ここで asynctask httpost?

は私のコードです

公共ボイド送信(ビューV) { 新しいsendtask()(実行します)。 }

private class sendtask extends AsyncTask<String,Void, String> { 


    String msg = msgTextField.getText().toString(); 
    String msg1 = spinner1.getSelectedItem().toString(); 
    String msg2 = spinner2.getSelectedItem().toString(); 


    protected String doInBackground(String...url) { 

      try { 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://10.0.2.2:80/test3.php"); 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);; 
       nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
       nameValuePairs.add(new BasicNameValuePair("name", msg)); 
       nameValuePairs.add(new BasicNameValuePair("gender",msg1)); 
       nameValuePairs.add(new BasicNameValuePair("age",msg2)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));     
       httpclient.execute(httppost); 

       msgTextField.setText(""); // clear text box 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
      } 
     return null; 
+0

Logcatのメッセージは何かを読んでください。WOuldは "私はエラーが発生しました"より役に立ちます –

+0

申し訳ありません、私はログcatを追加するのを忘れました。 – user1356620

答えて

1

OnPostExecuteは、ユーザーインターフェイスを(あなたがメソッドdoInBackground(文字列.. URL)でのユーザーインターフェイスを更新することはできません)を更新する必要がある場合、OnPostExecuteで受信されたパラメータは、値が返される使用あなたのケースがユーザーに関連しているかどうかではなく、doInBackground(String。.. url)によって投稿が行われたかどうかを通知します。

0

問題の特定と解決に役立つエラーも含めるべきです。おそらく次のようになります:

private class sendtask extends AsyncTask<String, Void, String> { 
    String msg = msgTextField.getText().toString(); 
    String msg1 = spinner1.getSelectedItem().toString(); 
    String msg2 = spinner2.getSelectedItem().toString(); 

    protected String doInBackground(String... url) { 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2:80/test3.php"); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); 
      nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("name", msg)); 
      nameValuePairs.add(new BasicNameValuePair("gender", msg1)); 
      nameValuePairs.add(new BasicNameValuePair("age", msg2)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 

      return ""; 
     } catch (ClientProtocolException e) { 
      return "ClientProtocolException"; 
     } catch (IOException e) { 
      return "IOException"; 
     } 
    } 

    protected void onPostExecute(String result) { 
     msgTextField.setText(result); // clear text box 
    } 
} 

重要な変更点は、msgTextField.setText("");が現在onPostExecute()にあり、表示するテキストをdoInBackground()から受信したことです。メインスレッドですべてのUI変更を行う必要があります。すなわち、doInBackground()にはありません。

+0

ありがとうございます。問題を解決する – user1356620