2012-11-25 2 views
4

나는 eclipse로 응용 프로그램을 개발 중입니다. 나는 phpmyadmin의 데이터베이스에 사용자가 입력 한 데이터를 저장하려고 시도했다. 불행히도 사용자가 제출 버튼을 클릭하면 응답이없고 데이터가 내 데이터베이스에 저장되지 않습니다.개발 된 안드로이드 응용 프로그램을 phpmyadmin에 연결할 수 없습니다.

import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.content.res.Configuration; 

public class UserRegister extends Activity { 

JSONParser jsonParser = new JSONParser(); 
EditText inputName; 
EditText inputUsername; 
EditText inputEmail; 
EditText inputPassword; 
RadioButton button1; 
RadioButton button2; 
Button button3; 
int success = 0; 

private static String url_register_user = "http://10.20.92.81/database/add_user.php"; 
private static final String TAG_SUCCESS = "success"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_user_register); 

    inputName = (EditText) findViewById(R.id.nameTextBox); 
    inputUsername = (EditText) findViewById(R.id.usernameTextBox); 
    inputEmail = (EditText) findViewById(R.id.emailTextBox); 
    inputPassword = (EditText) findViewById(R.id.pwTextBox); 

    Button button3 = (Button) findViewById(R.id.regSubmitButton); 

    button3.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 

      String name = inputName.getText().toString(); 
      String username = inputUsername.getText().toString(); 
      String email = inputEmail.getText().toString(); 
      String password = inputPassword.getText().toString(); 

      if (name.contentEquals("")||username.contentEquals("")||email.contentEquals("")||password.contentEquals("")) 
      { 
       AlertDialog.Builder builder = new AlertDialog.Builder(UserRegister.this); 


       builder.setMessage(R.string.nullAlert) 
        .setTitle(R.string.alertTitle); 

       builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }); 

      AlertDialog dialog = builder.show(); 
      } 

      // creating new product in background thread 
       RegisterNewUser(); 
     } 
    }); 
} 


    public void RegisterNewUser() 
    { 
    try 
    { 
     String name = inputName.getText().toString(); 
     String username = inputUsername.getText().toString(); 
     String email = inputEmail.getText().toString(); 
     String password = inputPassword.getText().toString(); 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("name", name)); 
     params.add(new BasicNameValuePair("username", username)); 
     params.add(new BasicNameValuePair("email", email)); 
     params.add(new BasicNameValuePair("password", password)); 

     // getting JSON Object 
     // Note that create product url accepts POST method 
     JSONObject json = jsonParser.makeHttpRequest(url_register_user, 
       "GET", params); 

     // check log cat for response 
     Log.d("Send Notification", json.toString()); 

     success = json.getInt(TAG_SUCCESS); 

     if (success == 1) 
     { 
      // successfully created product 
      Intent i = new Intent(getApplicationContext(), StudentLogin.class); 
      startActivity(i); 
      finish(); 
     } 

     else 
     { 
      // failed to register 

     } 
    } 

    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

    } 
} 

내 PHP 파일 :

<?php 
$response = array(); 

require_once __DIR__ . '/db_connect.php'; 

$db = new DB_CONNECT(); 


if (isset($_GET['name']) && isset($_GET['username']) && isset($_GET['email']) && isset($_GET['password'])) { 

$name = $_GET['name']; 
$username = $_GET['username']; 
$email = $_GET['email']; 
$password = $_GET['password']; 


// mysql inserting a new row 
$result = mysql_query("INSERT INTO register(name, username, email, password) VALUES('$name', '$username', '$email', '$password')"); 

// check if row inserted or not 
if ($result) { 
    // successfully inserted into database 
    $response["success"] = 1; 
    $response["message"] = "You are successfully registered to MEMS."; 

    // echoing JSON response 
    echo json_encode($response); 
} 
else { 
    // failed to insert row 
    $response["success"] = 0; 
    $response["message"] = "Oops! An error occurred."; 

    // echoing JSON response 
    echo json_encode($response); 
} 
} else { 
// required field is missing 
$response["success"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
} 
?> 

로그 고양이는 다음과 같다 : 여기

내 자바 파일입니다 첫째

11-25 10:37:46.772: I/Choreographer(638): Skipped 30 frames! The application may be doing too much work on its main thread. 

답변

1

, 자바 명명 규칙을 따라 계속 메소드 이름은 소문자로 시작합니다.

두 번째로 UI 스레드에서 네트워크 차단 작업을 수행 중이므로 NetworkOnMainThreadException이 표시되어야합니다. 너 그렇게하면 나쁜 시간을 보내 게 될거야.

모든 네트워크 코드를 비동기 작업 또는 적어도 별도의 스레드에 넣으십시오.

+0

대단히 감사합니다. 마침내 저는 거의 3 주 동안 직면 한 문제를 해결했습니다. 감사합니다! =) – user1850936

+0

당신이 솔루션을 얻게되면 알려주세요. PHP myadmin mysql 데이터베이스에서 세부 사항을 저장하는 방법은 무엇입니까? – Harsha

관련 문제