Egy androidos alkalmazást próbálok elkészíteni, elég nehezen megy. Az alkalmazás a regisztrációkor egyszerűen leáll egy ismeretlen hiba miatt. Hol lehet a hiba?
Android Studioval dolgozom.
Van egy BackgroundTaskom:
package com.example.kristf.php;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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 Kristóf on 2016. 06. 28..
*/
public class BackgroundTask extends AsyncTask<String, Void, String> {
String register_url = "localhost/loginapp/register.php";
Context ctx;
Activity activity;
ProgressDialog progressDialog;
AlertDialog.Builder builder;
public BackgroundTask(Context ctx){
this.ctx = ctx;
activity = (Activity)ctx;
}
@Override
protected void onPreExecute() {
builder = new AlertDialog.Builder(activity);
progressDialog = new ProgressDialog(ctx);
progressDialog.setTitle("Please wait");
progressDialog.setMessage("Connecting to server...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String method = params[0];
if(method.equals("register"))
{
try {
progressDialog.dismiss();
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String name = params[1];
String email = params[2];
String password = params[3];
String data = URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode("name", "UTF-8")+"&"+
URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode("email", "UTF-8")+"&"+
URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode("password", "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line=bufferedReader.readLine())!=null)
{
stringBuilder.append(line+"\n");
}
httpURLConnection.disconnect();
Thread.sleep(5000);
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("server_response");
JSONObject JO = jsonArray.getJSONObject(0);
String code = JO.getString("code");
String message = JO.getString("message");
if(code.equals("reg_true"))
{
showDialog("Registration Success", message, code);
}
else if(code.equals("reg_false"))
{
showDialog("Registration Failed", message, code);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void showDialog(String title, String message, String code)
{
builder.setTitle(title);
if(code.equals("reg_true")||code.equals("reg_false"))
{
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
activity.finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
}
Van egy LoginActivitym:
package com.example.kristf.php;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
TextView signup_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signup_text = (TextView)findViewById(R.id.sign_up);
signup_text.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
}
}
És végül egy RegisterActivitym:
package com.example.kristf.php;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RegisterActivity extends AppCompatActivity {
EditText Name,Email,Pass,ConPass;
Button reg_button;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Name = (EditText)findViewById(R.id.reg_name);
Email = (EditText)findViewById(R.id.reg_email);
Pass = (EditText)findViewById(R.id.reg_password);
ConPass = (EditText)findViewById(R.id.reg_con_password);
reg_button = (Button)findViewById(R.id.reg_button);
reg_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Name.getText().toString().equals("")||Email.getText().toString().equals("")||Pass.getText().toString().equals(""))
{
builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setTitle("Something went wrong...");
builder.setMessage("Please fill all the fields...");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}else if (!(Pass.getText().toString().equals(ConPass.getText().toString())))
{
builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setTitle("Something went wrong...");
builder.setMessage("Your passwords aren't matching...");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Pass.setText("");
ConPass.setText("");
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
else
{
BackgroundTask backgroundTask = new BackgroundTask(RegisterActivity.this);
backgroundTask.execute("register", Name.getText().toString(),Email.getText().toString(),Pass.getText().toString());
}
}
});
}
}
Ha megtaláltad a hibát, akkor írjál kérlek. Már 1-2 órája csak ülök felette, de nem értem! :(
2 tipp:
- Másold be a kódot pastebin-re, mert ott van 1) syntax highlighting, 2) behúzás
- Nézd meg, milyen exception-t ír a log - sokat szokott segíteni hibák felderítésénél.
Kapcsolódó kérdések:
Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!