我试图在类HttpPost中发出POST请求扩展AsyncTask实现了HttpUriRequest。
当我写作时:httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
它显示编译时错误:
The method setEntity(UrlEncodedFormEntity) is undefined for the type
ConTct.HttpPost. It says that I should make casting like this : ((HttpResponse) httppost).setEntity(new UrlEncodedFormEntity(nameValuePairs));
在运行时,它会打印:" ConTct$HttpPost cannot be cast to org.apache.http.HttpResponse
"。
请参阅我的代码:
public class ConTct extends Activity implements OnClickListener, OnTouchListener{
Button mButton;
EditText mFullName, mEmail, mDialZone, mPhone;
static WebView mWebView;
static ProgressBar mProgressBar;
EditText mBrokerId, mIP, mAreaPhonePrefix, mLastName, mPassWord, mCampaign, mSubCampaign, mCountryID, mCity, mAdress, mIsDemo;
public static final String myURL = "http://iphoneapp.optionrally.com:5757";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contct);
mButton=(Button)findViewById(R.id.SubmitRegisterButton);
mButton.setOnClickListener(this);
mButton.setOnTouchListener(this);
mFullName=(EditText)findViewById(R.id.firstName);
mEmail=(EditText)findViewById(R.id.email);
mDialZone=(EditText)findViewById(R.id.countryPhonePrefix);
mPhone=(EditText)findViewById(R.id.phoneNumber);
mWebView=(WebView)findViewById(R.id.myWebView);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new TheWebViewClient());
mWebView.loadUrl("http://media-dispatcher.com/tds/mobile/?mdaf=FormInApp");
mProgressBar=(ProgressBar)findViewById(R.id.myProgressBar);
GetCountryZipCode();
setUpInvisibleFields();
//Get a Tracker (should auto-report)
((AppManager) getApplication()).getTracker(AppManager.TrackerName.APP_TRACKER);
}//oncreate()
private static class TheWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
mProgressBar.setVisibility(View.GONE);
}
}//MyWebViewClient
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (GeneralValidation()==true){
Toast.makeText(getApplicationContext(), "Thank you for your registration", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), ThanksZuser.class);
startActivity(i);
finish();
//#HERE I AM SENDING THE DATA
grabURL(myURL);
}else{
Toast.makeText(getApplicationContext(), "*Fill the form correctly", Toast.LENGTH_SHORT).show();
}
}//onclick()
public boolean GeneralValidation(){
boolean result = false;
final String email = mEmail.getText().toString().trim();
final String pass = mFullName.getText().toString();
final String phone = mPhone.getText().toString();
if(!isValidEmail(email) || !isValidFullName(pass) || !isValidPhoneNumber(phone)){
if(!isValidEmail(email)){
mEmail.setError("Invalid email");
mEmail.setBackgroundColor(Color.RED);
mEmail.setTextColor(Color.BLUE);
}
if(!isValidFullName(pass)){
mFullName.setError("Invalid name. You have to input at least 3 characters");
mFullName.setBackgroundColor(Color.RED);
mFullName.setTextColor(Color.BLUE);
}
if(!isValidPhoneNumber(phone)){
mPhone.setError("Not Valid Number");
mPhone.setBackgroundColor(Color.RED);
mPhone.setTextColor(Color.BLUE);
}
}else{
result=true;
}
return result;
}//GeneralValidation
// validating phone
private boolean isValidPhoneNumber(String phone2) {
boolean check;
if(phone2.length() < 6 || phone2.length() > 13){
check = false;
}else{
check = true;
}
return check;
}
// validating email
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
// validating full name
private boolean isValidFullName(String pass) {
if (pass != null && pass.length() > 2) {
return true;
}
return false;
}
public String GetCountryZipCode(){
String CountryID="";
String CountryZipCode="";
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
//getNetworkCountryIso
CountryID= manager.getSimCountryIso().toUpperCase();
String[] rl=this.getResources().getStringArray(R.array.CountryCodes);
for(int i=0;i<rl.length;i++){
String[] g=rl[i].split(",");
if(g[1].trim().equals(CountryID.trim())){
CountryZipCode=g[0];
break;
}
}
mDialZone.setText( "+ " + CountryZipCode);
return CountryZipCode;
}//GetCountryZipCode
class HttpPost extends AsyncTask<String, Void, String> implements HttpUriRequest{
public HttpPost() {
super();
// TODO Auto-generated constructor stub
}
public HttpPost(String url) {
// TODO Auto-generated constructor stub
}
@Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
BufferedReader inBuffer = null;
String result = "fail";
String url = myURL;
// Create a new HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String l_ID, l_BrokerId, l_IP, l_SubBrokerID, l_AreaPhonePrefix, l_additionalPrefix, l_LastName, l_PassWord, l_terms, l_isLead, l_skinId, l_Campaign, l_languageId, l_SubCampaign, l_CountryID, l_City, l_Adress, l_IsDemo, l_birthday;
l_ID = "1010";
l_BrokerId = "4";
l_SubBrokerID="sub broker id";
l_IP="127.0.0.1";
l_AreaPhonePrefix= " my area phone prefix";
l_LastName= "my last name";
l_PassWord="my password";
l_languageId="2";
l_terms="on";
l_Campaign="1824";
l_SubCampaign="my sub campaign";
l_additionalPrefix="my prefix";
l_CountryID= "my country id";
l_City = " my city";
l_Adress= " my adress";
l_IsDemo="NO";
l_birthday="1900-01-01 00:00";
l_isLead = "yes";
l_skinId="my skin ID";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("<req>",""));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("id"), l_ID));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("brokerId"), l_BrokerId));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("subBrokerId"), l_SubBrokerID));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("<msg>"), ""));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("ip"), l_IP));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("password"), l_PassWord));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("countryId"), l_CountryID));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("campaign"), l_Campaign));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("languageId"), l_languageId));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("lastName"), l_LastName));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("additionalPrefix"), l_additionalPrefix));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("p"), l_birthday));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("birthday"), l_birthday));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("terms"), l_terms));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("subCampaign"), l_SubCampaign));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("isLead"), l_isLead));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("isDemo"), l_IsDemo));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("address"), l_Adress));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("city"), l_City));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("areaPhonePrefix"), l_AreaPhonePrefix));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("skinId"), l_skinId));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("mobilePrefix"), mDialZone.getText().toString()));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("mobileNumber"), mPhone.getText().toString()));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("firstName"), mFullName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair(PrepareXmlItemTag("email"), mEmail.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("</msg>", ""));
nameValuePairs.add(new BasicNameValuePair("</req>", ""));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Execute HTTP Post Request
try {
HttpResponse response = httpclient.execute(httppost);
System.out.println(response.toString());
result="got it";
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage();
}finally{
if (inBuffer != null) {
try {
inBuffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public RequestLine getRequestLine() {
// TODO Auto-generated method stub
return null;
}
@Override
public void addHeader(Header header) {
// TODO Auto-generated method stub
}
@Override
public void addHeader(String name, String value) {
// TODO Auto-generated method stub
}
@Override
public boolean containsHeader(String name) {
// TODO Auto-generated method stub
return false;
}
@Override
public Header[] getAllHeaders() {
// TODO Auto-generated method stub
return null;
}
@Override
public Header getFirstHeader(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public Header[] getHeaders(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public Header getLastHeader(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public HttpParams getParams() {
// TODO Auto-generated method stub
return null;
}
@Override
public ProtocolVersion getProtocolVersion() {
// TODO Auto-generated method stub
return null;
}
@Override
public HeaderIterator headerIterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public HeaderIterator headerIterator(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeHeader(Header header) {
// TODO Auto-generated method stub
}
@Override
public void removeHeaders(String name) {
// TODO Auto-generated method stub
}
@Override
public void setHeader(Header header) {
// TODO Auto-generated method stub
}
@Override
public void setHeader(String name, String value) {
// TODO Auto-generated method stub
}
@Override
public void setHeaders(Header[] headers) {
// TODO Auto-generated method stub
}
@Override
public void setParams(HttpParams params) {
// TODO Auto-generated method stub
}
@Override
public void abort() throws UnsupportedOperationException {
// TODO Auto-generated method stub
}
@Override
public String getMethod() {
// TODO Auto-generated method stub
return null;
}
@Override
public URI getURI() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isAborted() {
// TODO Auto-generated method stub
return false;
}
}
public void grabURL(String url){
new HttpPost().execute(url);
}
private String PrepareXmlItemTag(String tag){
return "<" + tag + ">" + "</" + tag + ">";
}
}//ConTct.class
请,请帮助我这个:)
答案 0 :(得分:0)
我是SIMpalm的PHP开发人员,我想你可以遵循这个解决方案......
而不是 HttpPost httppost = new HttpPost(url);使用此行
org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(url);