2013-04-22 1 views
0

나는 안드로이드 애플리케이션 개발에 초보자입니다. 나는 android.it에서 데이터베이스를 wamp 서버에 데이터를 저장하기위한 코딩을 했어 well.but 안드로이드에 PHP에서 성공 메시지를 받고 있지 않습니다. 내가 뭘했는지 모르겠다. 여기 를 코딩안드로이드 에뮬레이터에 메시지 상자가 나타나지 않습니다.

package com.example.loga; 


import android.os.Bundle; 

import android.app.Activity; 
import android.app.AlertDialog; 

import android.view.Menu; 
import android.widget.Button; 


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.ProgressDialog; 


import android.os.AsyncTask; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 


public class MainActivity1 extends Activity { 

    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
     EditText inputuserid; 
     EditText inputserverid; 

     private static String url_register = "http://10.0.2.2/android_connect/register.php"; 
     // JSON Node names 
     private static final String TAG_SUCCESS = "success"; 





    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_activity1); 
     // Edit Text 
     inputuserid = (EditText) findViewById(R.id.editText1); 
     inputserverid= (EditText) findViewById(R.id.editText2); 
     Button button1 = (Button) findViewById(R.id.button1); 
     button1.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) { 
       // creating new product in background thread 
       new add().execute(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_activity1, menu); 
     return true; 
    } 
    class add extends AsyncTask<String, String,String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MainActivity1.this); 
      pDialog.setMessage("your Registration is processing..wait for few sec.."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 
      String userid = inputuserid.getText().toString(); 
      String serverid = inputserverid.getText().toString(); 

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("userid",userid)); 
      params.add(new BasicNameValuePair("serverid", serverid)); 


      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_register, 
        "POST", params); 
      // check log cat for response 
      Log.d("Create Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        AlertDialog alertDialog; 
        alertDialog = new AlertDialog.Builder(MainActivity1.this).create(); 
        alertDialog.setTitle("password verification.."); 
        alertDialog.setMessage("success.!!"); 
        alertDialog.show(); 

        finish(); 
       } else { 
        // failed to create product 
       } 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 



      return null; 

     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
     } 

    } 

} 

파일 내 PHP 코딩 내가 database.help 나에 데이터를 저장 한 후 에뮬레이터에서 메시지 상자를 표시 할

<?php 
$response = array(); 
// check for required fields 
if (isset($_POST['userid']) && isset($_POST['serverid'])) 
{ 

    $userid = $_POST['userid']; 
    $serverid= $_POST['serverid']; 
     // include db connect class 
    require_once __DIR__ . '/db_connect.php'; 

    // connecting to db 
    $db = new DB_CONNECT(); 

    // mysql inserting a new row 
    $result = mysql_query("INSERT INTO opass(User_ID, Server_ID) VALUES('$userid', '$serverid')"); 

    // check if row inserted or not 
    if ($result) 
{ 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = " Registered successfully"; 

     // 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); 
} 

?> 

입니다 된 .java 내이다. 감사합니다 :)

+0

문제는 무엇입니까, 대화 상자를 표시 하시겠습니까? 또는 서버에서 성공 응답을 받으시겠습니까? –

+0

실제로 코드가 잘못되었는지 어디에서 알지 못합니다. 서버에서 성공 응답을 얻었습니다. 메시지 상자를 표시하고 싶습니다. –

+0

디버거에서 한 가지 더 확인하고 문제가있는 곳에서 확인하고 int success = json.getInt (TAG_SUCCESS); if (성공 == 1) {........... 여기를 확인하십시오 1이 누락 되었습니까? – Rohit

답변

0

doInBackground (String ... args) 메서드에서 UI 수정을 수행 할 수 없습니다. onPostExecute (String file_url) 메소드에 대화 상자를 작성하고 표시하는 논리를 이동하십시오.

doInBackground()에서 부울 상태를 반환하고 onPostExecute()에서 상태를 확인한 다음 이에 따라 대화 상자를 표시하십시오.

관련 문제