Java AsyncTask中的doInBackground()无法建立连接
我正在尝试为我的android应用程序进行注册活动。我正在使用AsyncTask来执行此操作。但是代码似乎没有连接到我服务器上的数据库(不是本地主机),因为它没有显示在Android调试器和监视器中与服务器的任何连接。帮忙Java AsyncTask中的doInBackground()无法建立连接,java,android,url,android-asynctask,httpurlconnection,Java,Android,Url,Android Asynctask,Httpurlconnection,我正在尝试为我的android应用程序进行注册活动。我正在使用AsyncTask来执行此操作。但是代码似乎没有连接到我服务器上的数据库(不是本地主机),因为它没有显示在Android调试器和监视器中与服务器的任何连接。帮忙 private void registrationProcess(String name, String email, String mob, String password) { String urlSuffix=“?userName=“+name+”&userMobil
private void registrationProcess(String name, String email, String mob, String password)
{
String urlSuffix=“?userName=“+name+”&userMobile=“+mob+”&userEmail=“+email+”&userPassword=“+password;
类注册过程扩展了异步任务{
进程对话框加载寄存器;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loadingRegister=ProgressDialog.show(RegisterActivity.this,“正在注册…”,“请稍候…”,true,true);
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串s=params[0];
BufferedReader BufferedReader=null;
试一试{
URL=新URL(注册URL+s);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.connect();
bufferedreader=新的bufferedreader(新的InputStreamReader(con.getInputStream());
字符串结果=bufferedreader.readLine();
con.disconnect();
返回结果;
}
捕获(例外情况除外)
{
System.out.println(例如getMessage());
返回null;
}
}
}
RegistrationProcess rgp=新的RegistrationProcess();
rgp.execute(urlSuffix);
}你至少应该展示一下你的操作方式,否则,你的问题就解决不了了。当然,我只是添加了一点。尝试添加catch this:System.out.println(ex.getMessage())或其他内容来查看异常,因为你没有处理异常,只是返回null……没有异常发生。它只是没有连接到服务器。请检查是否在清单文件中添加INTERNET权限
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);
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();
}
} //