Android Java错误类型不匹配和未定义的方法?

时间:2014-06-13 13:03:12

标签: java android

以下是我的代码:     包com.loginform;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;


public class MainActivity extends ActionBarActivity implements OnClickListener {

Button login;
EditText username, password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setup();
    }
private void setup() {
    // TODO Auto-generated method stub
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);
    status = (TextView) findViewById(R.id.tvstatus);
    login.setOnClickListener(this);
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.login:

        login();

        break;
    }
}
private void login() {
    // TODO Auto-generated method stub
    try{
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost ("http://10.0.2.2/android/check.php");
    //add the data
    nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("username",     username.getText().toString().trim()));
    nameValuePairs.add(new BasicNameValuePair("password",     password.getText().toString().trim()));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    //Execute Http Post Request
    response = httpclient.execute(httppost);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    final String reponse = httpclient.execute(httppost, responseHandler);

    tv.SetText(""+response);
    if(response.equalsIgnoreCase("User Found")){
        startActivity(new Intent(MainActivity.this.UserPage.class));
    }
    }catch(Exception e){
        e.printStackTrace();
    }
}

}

我遇到的错误如下:

/*error:Type mismatch: cannot convert from BasicResponseHandler to    ResponseHandler<R.string>*/
ResponseHandler<String> responseHandler = new BasicResponseHandler();

/*error:tv cannot be resolved */
tv.SetText(""+response);

/*error:The method equalsIgnoreCase(String) is undefined for the type HttpResponse*/
if(response.equalsIgnoreCase("User Found"))

/*error:UserPage cannot be resolved or is not a field*/
startActivity(new Intent(MainActivity.this.UserPage.class));

我已经尝试了所有快速修复,但只是得到更多错误,任何帮助将不胜感激!请不要我添加TextView电视;其他人定义的地方,但仍然会出现错误。

1 个答案:

答案 0 :(得分:1)

试试这种方式

BasicResponseHandler responseHandler = new BasicResponseHandler();

而不是

ResponseHandler<String> responseHandler = new BasicResponseHandler();

要处理来自 httpGet 响应。您可以使用ResponseHandlerBasicResponseHandler

两者之间的区别在于ResponseHandler可以返回任何类型的变量,而BasicResponseHandler只能返回String。在大多数情况下,您将使用BasicResponseHandler,这更常见。以下是获得回复的两种方式:

ResponseHandler<String> responseHandler = new BasicResponseHandler();

BasicResponseHandler responseHandler = new BasicResponseHandler();