我正在尝试为我的Android应用程序进行注册活动。我正在使用AsyncTask来执行此操作。但是代码似乎没有连接到我的服务器上的数据库(而不是localhost),因为它没有显示Android调试器和监视器中与服务器的任何连接。帮助
private void registrationProcess(String name, String email, String mob, String password)
{
String urlSuffix = "?userName=" + name + "&userMobile=" + mob + "&userEmail=" + email + "&userPassword=" + password;
class RegistrationProcess extends AsyncTask<String, Void, String> {
ProgressDialog loadingRegister;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingRegister = ProgressDialog.show(RegisterActivity.this, "Registering...", "Please wait...", true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferedreader = null;
try {
URL url = new URL(REGISTER_URL + s);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
bufferedreader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result = bufferedreader.readLine();
con.disconnect();
return result;
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
return null;
}
}
}
RegistrationProcess rgp = new RegistrationProcess();
rgp.execute(urlSuffix);
}
答案 0 :(得分:0)
Try this code
@Override
protected String doInBackground(String... params) {
// entrer URL ou le fichier php réside
String s = params[0];
try {
url = new URL(REGISTER_URL+s);
} catch (MalformedURLException e) {
e.printStackTrace();
return "exception";
}
try{
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNEXTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
// apprend parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("somthing",params[0 or 1 or ...]);
String query = builder.build().getEncodedQuery();
// ouvrir une connexion pour envoyer des informations
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
}catch (IOException e){
e.printStackTrace();
return "Exception";
}
try{
int response_code = conn.getResponseCode();
if(response_code == HttpURLConnection.HTTP_OK){
//Lire data envoyer depuis le serveur
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine())!=null){
result.append(line);
}
//envoyer data to "" onPostExecute "" methode
return(result.toString());
}
else{
return("Echec...");
}
}catch (IOException e){
e.printStackTrace();
return "Exception";
}
finally{
conn.disconnect();
}
} //