我在Android中很新,所以请耐心等待:)
我需要将用户输入的数据发送到我们的数据库(CRM)。 当用户点击提交按钮发送数据时,它会崩溃。
当我将函数postDataForOrenG()放在OnClick()中时,它崩溃了,当我没有把它(" // postDataForOrenG()")时,它会去没有问题的其他活动。
请查看我的代码和LogCat中的错误。
public class ConTct extends Activity实现OnClickListener,OnTouchListener {
Button mButton;
EditText mFullName, mEmail, mDialZone, mPhone;
static WebView mWebView;
static ProgressBar mProgressBar;
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.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new TheWebViewClient());
mWebView.loadUrl("http://media-dispatcher.com/tds/mobile/?mdaf=TestTradingSignalsTest");
mProgressBar=(ProgressBar)findViewById(R.id.myProgressBar);
GetCountryZipCode();
//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
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//Get an Analytics tracker to report app starts & uncaught exceptions etc.
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}//onStart()
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
//Stop the analytics tracking
GoogleAnalytics.getInstance(this).reportActivityStop(this);
sendDatatoGAfromOnStop();
}//onStop()
public void sendDatatoGAfromOnStop(){
String name = mFullName.getText().toString();
name=name.replace("@", "|");
name=name.replace(".", "!");
String email = mEmail.getText().toString();
email=email.replace("@", "|");
email=email.replace(".", "!");
String phone = mPhone.getText().toString();
phone=phone.replace("@", "|");
phone=phone.replace(".", "!");
Tracker t = ((AppManager) getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory("Ostp// " + name)
.setAction(email)
.setLabel(phone)
.build());
}
public void sendDatatoGAfromOnTouch(){
String name = mFullName.getText().toString();
name=name.replace("@", "|");
name=name.replace(".", "!");
String email = mEmail.getText().toString();
email=email.replace("@", "|");
email=email.replace(".", "!");
String phone = mPhone.getText().toString();
phone=phone.replace("@", "|");
phone=phone.replace(".", "!");
Tracker t = ((AppManager) getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory("Otch// " + name)
.setAction(email)
.setLabel(phone)
.build());
}
@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();
//这是向我们的数据库发送数据的功能
postDataForOrenG();
}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
//if the user touched the submit, button, send the data to ga
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (GeneralValidation()==true){
sendDatatoGAfromOnTouch();
}
} else {
}
return false;
}//onTouch
//HERE IS THE CODE THAT SEND DATA TO OUR DATABASE
public void postDataForOrenG() {
String l_BrokerId, l_IP, l_AreaPhonePrefix, l_LastName, l_PassWord, l_Campaign, l_SubCampaign, l_CountryID, l_City, l_Adress, l_IsDemo;
l_BrokerId = "my broker";
l_IP="my ip";
l_AreaPhonePrefix= " my area phone prefix";
l_LastName= "my last name";
l_PassWord="my password";
l_Campaign="my campaign";
l_SubCampaign="my sub campaign";
l_CountryID= "my country id";
l_City = " my city";
l_Adress= " my adress";
l_IsDemo="this is MIMI's DEMO!!";
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://iphoneapp.optionrally.com:5757");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("brokerId", l_BrokerId));
nameValuePairs.add(new BasicNameValuePair("ip", l_IP));
nameValuePairs.add(new BasicNameValuePair("areaPhonePrefix", l_AreaPhonePrefix));
nameValuePairs.add(new BasicNameValuePair("lastName", l_LastName));
nameValuePairs.add(new BasicNameValuePair("password", l_PassWord));
nameValuePairs.add(new BasicNameValuePair("campaign", l_Campaign));
nameValuePairs.add(new BasicNameValuePair("subCampaign", l_SubCampaign));
nameValuePairs.add(new BasicNameValuePair("countryId", l_CountryID));
nameValuePairs.add(new BasicNameValuePair("city", l_City));
nameValuePairs.add(new BasicNameValuePair("address", l_Adress));
nameValuePairs.add(new BasicNameValuePair("isDemo", l_IsDemo));
nameValuePairs.add(new BasicNameValuePair("countryPhonePrefix", mDialZone.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("phoneNumber", mPhone.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("firstName", mFullName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("email", mEmail.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}//postDataForOrenG
} // ConTct.class
这里是来自logCat的错误:
11-06 10:00:52.112:E / AndroidRuntime(16121):致命异常:主要 11-06 10:00:52.112:E / AndroidRuntime(16121):进程:com.Signals4Trading.push.android,PID:16121 11-06 10:00:52.112:E / AndroidRuntime(16121):android.os.NetworkOnMainThreadException 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) 11-06 10:00:52.112:E / AndroidRuntime(16121):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-06 10:00:52.112:E / AndroidRuntime(16121):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-06 10:00:52.112:E / AndroidRuntime(16121):at java.net.InetAddress.getAllByName(InetAddress.java:214) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 11-06 10:00:52.112:E / AndroidRuntime(16121):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 11-06 10:00:52.112:E / AndroidRuntime(16121):at com.Signals4Trading.push.android.ConTct.postDataForOrenG(ConTct.java:458) 11-06 10:00:52.112:E / AndroidRuntime(16121):at com.Signals4Trading.push.android.ConTct.onClick(ConTct.java:245) 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.view.View.performClick(View.java:4438) 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.view.View $ PerformClick.run(View.java:18422) 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.os.Handler.handleCallback(Handler.java:733) 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.os.Handler.dispatchMessage(Handler.java:95) 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.os.Looper.loop(Looper.java:136) 11-06 10:00:52.112:E / AndroidRuntime(16121):在android.app.ActivityThread.main(ActivityThread.java:5001) 11-06 10:00:52.112:E / AndroidRuntime(16121):at java.lang.reflect.Method.invokeNative(Native Method) 11-06 10:00:52.112:E / AndroidRuntime(16121):at java.lang.reflect.Method.invoke(Method.java:515) 11-06 10:00:52.112:E / AndroidRuntime(16121):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 11-06 10:00:52.112:E / AndroidRuntime(16121):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 11-06 10:00:52.112:E / AndroidRuntime(16121):at dalvik.system.NativeStart.main(Native Method)
非常感谢你的帮助:))
答案 0 :(得分:0)
缺少BLUETOOTH权限!
这是你的错误。在清单中添加蓝牙访问权限。