Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,732,310 Members 45,372 Now Online
XDA Developers Android and Mobile Development Forum

[Q] Failed to load details from database using java and php

Tip us?
 
chin90
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 2
Join Date: Mar 2014
Post [Q] Failed to load details from database using java and php

I have developed an Android apps which includes a function of writing memo.
After user clicks on the view memo, there is a list of memo name. User can click on the specific memo to display the details in edit form.
I have tested the php code by using the browser, it returns the correct memo details.
But unfortunately, after user clicks on the selected memo, the apps has stopped working.

here is the java code:

Code:
public class EditMemoActivity extends Activity {
 
    EditText editMemoName;
    EditText editMemoDetails;
    Button btnSaveMemo;
    Button btnDeleteMemo;
 
    String pid;
 
    private ProgressDialog pDialog;
 
    JSONParser jsonParser = new JSONParser();
    static String URL = new ServerURL().getURL();
    
    private static final String url_memo_details = URL+"database/get_memo_details.php";
 
    private static final String url_update_memo = URL+"database/update_memo.php";
 
    private static final String url_delete_memo = URL + "database/delete_memo.php";
 
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MEMO = "memo";
    private static final String TAG_PID = "pid";
    private static final String TAG_MEMO_NAME = "memo_name";
    private static final String TAG_MEMO_DETAILS = "memo_details";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_memo);
 
        btnSaveMemo = (Button) findViewById(R.id.btnSaveMemo);
        btnDeleteMemo = (Button) findViewById(R.id.btnDeleteMemo);
 
        Intent i = getIntent();
 
        pid = i.getStringExtra(TAG_PID);
 
        new GetMemoDetails().execute();
 
        btnSaveMemo.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View arg0) {
                new SaveMemoDetails().execute();
            }
        });
 
        btnDeleteMemo.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View arg0) {
                new DeleteMemo().execute();
            }
        });
 
    }
 
    class GetMemoDetails extends AsyncTask<String, String, String> {
 
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(EditMemoActivity.this);
            pDialog.setMessage("Loading memo details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        protected String doInBackground(String... params) {
 
            runOnUiThread(new Runnable() {
                public void run() {
                    int success;
                    try {
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("pid", pid));
 
                        JSONObject json = jsonParser.makeHttpRequest(
                                url_memo_details, "GET", params);
 
                        Log.d("Single Memo Details", json.toString());
 
                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {
                            JSONArray memoObj = json
                                    .getJSONArray(TAG_MEMO); // JSON Array
 
                            JSONObject memo = memoObj.getJSONObject(0);
                            ;
                            editMemoName = (EditText) findViewById(R.id.editTextEditMemoName);
                            editMemoDetails = (EditText) findViewById(R.id.editTextEditMemoDetails);
                            
                            editMemoName.setText(memo.getString(TAG_MEMO_NAME));
                            editMemoDetails.setText(memo.getString(TAG_MEMO_DETAILS));
                        }else{
                            // memo with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
 
            return null;
        }
 
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }
    }
    }
here is the log file:

Code:
03-31 11:05:45.986: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.001: D/AbsListView(27729): unregisterIRListener() is called 
    03-31 11:05:46.026: D/All Memos:(27729): {"memo":[{"memo_name":"To Buy List","memo_details":"maggi, umbrella","pid":"3"},{"memo_name":"To Do List","memo_details":"visit legoland","pid":"4"},{"memo_name":"2014.03.04","memo_details":"tmr rmb go pc fait","pid":"5"},{"memo_name":"123","memo_details":"sadf","pid":"6"},{"memo_name":"123","memo_details":"sadf","pid":"7"},{"memo_name":"123","memo_details":"sadf","pid":"8"}],"success":1}
    03-31 11:05:46.051: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.056: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.056: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.061: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.076: D/AbsListView(27729): unregisterIRListener() is called 
    03-31 11:05:46.136: D/AbsListView(27729): unregisterIRListener() is called 
    03-31 11:05:46.146: E/ViewRootImpl(27729): sendUserActionEvent() mView == null
    03-31 11:05:46.421: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.421: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.421: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:46.421: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.636: D/ProgressBar(27729): setProgress = 0
    03-31 11:05:48.636: D/ProgressBar(27729): setProgress = 0, fromUser = false
    03-31 11:05:48.636: D/ProgressBar(27729): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
    03-31 11:05:48.681: D/AbsListView(27729): unregisterIRListener() is called 
    03-31 11:05:48.686: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.721: D/ProgressBar(27729): updateDrawableBounds: left = 0
    03-31 11:05:48.721: D/ProgressBar(27729): updateDrawableBounds: top = 0
    03-31 11:05:48.721: D/ProgressBar(27729): updateDrawableBounds: right = 96
    03-31 11:05:48.721: D/ProgressBar(27729): updateDrawableBounds: bottom = 96
    03-31 11:05:48.726: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.726: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.726: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.726: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.731: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.871: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.871: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.881: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.886: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.886: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.886: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.886: D/TextLayoutCache(27729): Enable myanmar Zawgyi converter
    03-31 11:05:48.906: D/AndroidRuntime(27729): Shutting down VM
    03-31 11:05:48.906: W/dalvikvm(27729): threadid=1: thread exiting with uncaught exception (group=0x41e31700)
    03-31 11:05:48.916: E/AndroidRuntime(27729): FATAL EXCEPTION: main
    03-31 11:05:48.916: E/AndroidRuntime(27729): android.os.NetworkOnMainThreadException
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at libcore.io.IoBridge.connect(IoBridge.java:112)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at java.net.Socket.connect(Socket.java:842)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at com.example.test.JSONParser.makeHttpRequest(JSONParser.java:62)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at com.example.test.EditMemoActivity$GetMemoDetails$1.run(EditMemoActivity.java:99)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at android.os.Handler.handleCallback(Handler.java:730)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at android.os.Handler.dispatchMessage(Handler.java:92)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at android.os.Looper.loop(Looper.java:137)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at android.app.ActivityThread.main(ActivityThread.java:5493)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at java.lang.reflect.Method.invokeNative(Native Method)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at java.lang.reflect.Method.invoke(Method.java:525)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
    03-31 11:05:48.916: E/AndroidRuntime(27729): 	at dalvik.system.NativeStart.main(Native Method)
    03-31 11:07:31.716: I/Process(27729): Sending signal. PID: 27729 SIG: 9
here is the php code:

Code:
<?php
 
     // array for JSON response
    $response = array();
 
    // include db connect class
    require_once __DIR__ . '/db_connect.php';
 
    // connecting to db
    $db = new DB_CONNECT();
 
    // check for post data
    if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];
 
    $result = mysql_query("SELECT *FROM memo WHERE pid = $pid");
 
    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {
 
            $result = mysql_fetch_array($result);
 
            $memo = array();
            $memo["pid"] = $result["pid"];
            $memo["memo_name"] = $result["memo_name"];
            $memo["memo_details"] = $result["memo_details"];
            // success
            $response["success"] = 1;
 
            // user node
            $response["memo"] = array();
 
            array_push($response["memo"], $memo);
 
            // echoing JSON response
            echo json_encode($response);
        } else {
            
            $response["success"] = 0;
            $response["message"] = "No memo found";
 
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        $response["success"] = 0;
        $response["message"] = "No memo found";
 
        // echo no users JSON
        echo json_encode($response);
    }
    } else {
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
    } 
    ?>
 
deanwray
Old
#2  
deanwray's Avatar
Senior Member
Thanks Meter 407
Posts: 1,089
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by chin90 View Post
I have developed an Android apps which includes a function of writing memo.
After user clicks on the view memo, there is a list of memo name. User can click on the specific memo to display the details in edit form.
I have tested the php code by using the browser, it returns the correct memo details.
But unfortunately, after user clicks on the selected memo, the apps has stopped working.

Your problem

03-31 11:05:48.916: E/AndroidRuntime(27729): android.os.NetworkOnMainThreadException

google for info. In short, the reason is in the name of the exception Need to do networking on a seperate (to the UI) thread
The Following User Says Thank You to deanwray For This Useful Post: [ Click to Expand ]
 
chin90
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 2
Join Date: Mar 2014
Quote:
Originally Posted by deanwray View Post
Your problem

03-31 11:05:48.916: E/AndroidRuntime(27729): android.os.NetworkOnMainThreadException

google for info. In short, the reason is in the name of the exception Need to do networking on a seperate (to the UI) thread
Thanks I have solved the problem
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes