我正在尝试将我的应用程序连接到在线数据库,我已经将所有的php文件都工作了,但是我的应用程序没有连接到数据库。我必须做简单的数据插入和数据检索。我继续得到空指针异常。
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RegOneActivity extends Activity {
Button okaBtn,regbtn;
EditText phno,password;
String phone,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg_one);
password = (EditText) findViewById(R.id.passwordedittext);
okaBtn =(Button) findViewById(R.id.okbtn);
phno = (EditText) findViewById(R.id.editTextpass1);
regbtn = (Button) findViewById(R.id.createbtn);
regbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(RegOneActivity.this, PasswordActivity.class);
startActivity(in);
}
});
okaBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
phone = phno.getText().toString();
pass = password.getText().toString();
String method = "login";
/*RegisterBackgroung backGround = new RegisterBackgroung(getApplicationContext());*/
RegisterBackgroung backGround = new RegisterBackgroung(RegOneActivity.this);
backGround.execute(method, phone, pass);
Intent in = new Intent(RegOneActivity.this, MapActivity.class);
startActivity(in);
RegOneActivity.this.finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.reg_one, menu);
return true;
}
@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);
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PasswordActivity extends Activity {
Button btn;
EditText password1,password2,phone;
String pass1,pass2,phonenumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
btn =(Button) findViewById(R.id.proceed1act);
password1 = (EditText) findViewById(R.id.editTextpass1);
phone = (EditText) findViewById(R.id.phoneedt);
password2 = (EditText) findViewById(R.id.editTextpass2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pass1 = password1.getText().toString();
pass2 = password2.getText().toString();
phonenumber = phone.getText().toString();
String method= "register";
RegisterBackgroung backtask = new RegisterBackgroung(PasswordActivity.this);
backtask.execute(method,phonenumber,pass1);
Intent in = new Intent(PasswordActivity.this, RegOneActivity.class);
startActivity(in);
PasswordActivity.this.finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.password, menu);
return true;
}
@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);
}
}
RegisterBackground.java
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.widget.Toast;
public class RegisterBackgroung extends AsyncTask<String, Void, String>{
Context ctx;
AlertDialog diag;
SharedPreferences sp;
Editor edt;
public RegisterBackgroung(Context ctx) {
this.ctx = ctx;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String urlregister = "http://www.locationsharing.16mb.com/register.php";
String login = "http://www.locationsharing.16mb.com/retrieve.php";
String method = params[0];
if(method.equals("register"))
{
String phone = params[1];
String pass = params[2];
try {
URL reg_url = new URL(urlregister);
HttpURLConnection httpUrlConnection = (HttpURLConnection) reg_url.openConnection();
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
OutputStream os = httpUrlConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
String data = URLEncoder.encode("phonenumber", "UTF-8")+"="+URLEncoder.encode(phone, "UTF-8")+"&"+
URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(pass, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
InputStream is = httpUrlConnection.getInputStream();
is.close();
return "Registration Successful!";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(method.equals("login")){
String phone = params[1];
String pass = params[2];
try {
URL url = new URL(login);
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
OutputStream os = httpUrlConnection.getOutputStream();
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("phonenumber", "UTF-8")+"="+URLEncoder.encode(phone, "UTF-8")+"&"+
URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(pass, "UTF-8");
bw.write(data);
bw.flush();
bw.close();
os.close();
InputStream is = httpUrlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
String response = "";
String line = "";
while((line = br.readLine())!=null)
{
response += line;
}
br.close();
is.close();
httpUrlConnection.disconnect();
return response;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
diag.setTitle("Login Information");
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result.equals("Registration Successful!"))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else
{
diag.setMessage(result);
diag.show();
}
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lavisor.locationsharing2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<permission
android:name="com.testing.svma.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.testing.svma.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegOneActivity"
android:label="@string/title_activity_reg_one" >
</activity>
<activity
android:name=".PasswordActivity"
android:label="@string/title_activity_password" >
</activity>
<activity
android:name=".PassActivity"
android:label="@string/title_activity_pass" >
</activity>
<activity
android:name=".MapActivity"
android:label="@string/title_activity_map" >
</activity>
<activity
android:name=".SelectCircleActivity"
android:label="@string/title_activity_select_circle" >
</activity>
<activity
android:name=".ShareLocationActivity"
android:label="@string/title_activity_share_location" >
</activity>
<activity
android:name=".FavouritesActivity"
android:label="@string/title_activity_favourites" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAZx8eHxiQ5ZRgsDstuyseUKQ_PaH8rlps" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
答案 0 :(得分:0)
我忘了在异步任务中覆盖OnProgressUpdate:P和Now,它完美地工作..