0
我在这里的第一篇文章,所以如果它在错误的地方,或者如果我有一两件事情错了,请原谅。此外,我是一个认真的初学者,所以假设我知道的很少。 谢谢... 我正在Android工作室创建一个简单的应用程序,我试图在在线服务器上注册用户。 我得到它的工作(它注册用户在服务器上),但现在我试图添加进度对话框,以便当应用程序连接到服务器时,这将显示。 我下面的代码似乎工作 - 然后在对话框消失,出现直线距离,但最后我得到一个消息说:“不幸的是登录登录注册已停止”Android工作室progressDialog在asynctask
任何想法有什么错我的代码...这里是
package com.example.android.loginregister;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
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.URL;
import java.net.URLEncoder;
/**
* Created by Jon on 2015/09/15.
*/
public class BackgroundTask extends AsyncTask<String, Integer, String> {
//declares a context variable
Context ctx;
//declares an alert dialog object
AlertDialog alertDialog;
ProgressDialog progressDialog;
//declare constructor will need to be passed a context
BackgroundTask(Context ctx){
this.ctx = ctx;
progressDialog = new ProgressDialog(ctx);
}
//in this me`enter code here`thod (which happens before execution of the object) the alert dialog object is instantiated
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information....");
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
//set up url connections
String reg_url = "http://jontestdb.webege.com/register.php";
String login_url = "http://jontestdb.webege.com/login.php";
String method = params[0];
if (method.equals("register")){
String name = params[1];
String user_name = params[2];
String user_pass = params[3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) 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("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") +"&"+
URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") +"&"+
URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Registration success...";
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} ;
}
else if(method.equals("login")){
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//defines communication method
httpURLConnection.setRequestMethod("POST");
//need to send info to the server
httpURLConnection.setDoOutput(true);
//gets response from server
httpURLConnection.setDoInput(true);
//now need to get output stream from connection so first create output stream object
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
//need to specify data - used the post method so have to encode data
String data = URLEncoder.encode("login_name", "UTF-8")+"="+URLEncoder.encode(login_name, "UTF-8")+"&"+
URLEncoder.encode("login_pass", "UTF-8")+"="+URLEncoder.encode(login_pass, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//get response from server
InputStream inputStream = httpURLConnection.getInputStream();
//need buffered reader now
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
//now need to get information from bufferedreader
String response = "";
String line = "";
//this is saying that if buffered reader.read line is not null then get the info from buffered reader
//the data obtained from the read line method and stored in the line variable is the added the response variable
//seems as though bufferedreader contains the query result
while ((line = bufferedReader.readLine()) != null){
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
//this returns the response which should be the name of the user
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Registration success...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else{
alertDialog.setMessage(result);
alertDialog.show();
}
if (progressDialog != null){
progressDialog.dismiss();}
}
}
您可以请发布您的logcat –
嗨。我现在似乎不能重复这个错误,我想我可能已经解决了它没有意义哈哈。如果我设法做到,我会在这里发布logcat。但现在让我们假设这是帖子解决。感谢您的回复Arsal Imam,并对任何不便表示歉意。我相信我会沿着我的方式偶然遇到更多问题。再次感谢。 – Jon