0

我是 android 新手,正在开发一个 android 应用程序,该应用程序在单击按钮时将搜索项(由用户提供到 EditText)发布到服务器,该项目被发布到服务器。我正在使用 Asynctask 类。我有错误findViewById(未定义)。我的问题是在哪里放置 onclick 方法和视图的引用。这里是代码

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

  private static final int REGISTRATION_TIMEOUT = 3 * 1000;
    private static final int WAIT_TIMEOUT = 30 * 1000;
    private final HttpClient httpclient = new DefaultHttpClient();
     final HttpParams params = httpclient.getParams();
      HttpResponse response;

      Button Submit = (Button) findViewById(R.id.submitButton);
      EditText textvalue = (EditText)findViewById(R.id.searcheditText);

      //Onclick Listener
      Submit.setOnClickListener(onClickListener) 
      private OnClickListener onClickListener = new OnClickListener() {
          @Override
          public void onClick(final View v) {
              switch(v.getId()){
                  case R.id.submitButton:
                break;
          }
      };

    protected void onPreExecute() {
    //any code    

    }

@Override
protected String doInBackground(String... arg0) {
    String URL = "url";
    String username = "abc";
    String password = "xyz";


    try {
        HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
        ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);

        HttpPost httpPost = new HttpPost(URL);

        //Any other parameters you would like to set
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username",username));
        nameValuePairs.add(new BasicNameValuePair("password",password));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        //Response from the Http Request
        response = httpclient.execute(httpPost);

        StatusLine statusLine = response.getStatusLine();
        //Check the Http Request for success
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();

        }
        else{
            //Closes the connection.
            Log.w("HTTP1:",statusLine.getReasonPhrase());
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }


    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }catch (Exception e) {

    }
    return null;


}

protected void onCancelled() {

}

protected void onPostExecute(String content) {

}


private void connecttopostdata() {  
    Server_Post task = new Server_Post();  
    task.execute(textvalue.getText().toString());  
  }

 }

响应在 xml 中,我还想在 listviews 上显示响应。提前致谢。

4

1 回答 1

0

您需要一个活动类的上下文才能从 findviewbyid 获取按钮。创建一个构造函数并从那里找到您的视图。喜欢..

// give context from where you are calling your AsyncTask
 public Server_Post (Context context)
{
    Button Submit = (Button) context.findViewById(R.id.submitButton);
    EditText textvalue = (EditText)context.findViewById(R.id.searcheditText);
}
于 2013-02-15T14:07:49.163 回答