0

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:

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:

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:

<?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);
} 
?>
user1850936
  • 129
  • 3
  • 18
  • what's this to do with phpmyadmin? which is just one of many php scripts which can be used to manage a db –  Mar 31 '14 at 03:37
  • @Dagon i am just to let people know that I am using the phpmyadmin to handle my database. I will remove it from the tag, thanks. – user1850936 Mar 31 '14 at 03:41
  • You are getting the same error as your prior question: NetworkOnMainThredException. Using runOnUiThread with the network call inside it defeats the use of an AsyncTask. Look at: http://stackoverflow.com/questions/9719234/android-calling-the-methods-on-ui-thread-from-asynctask-doinbackground-method – Morrison Chang Mar 31 '14 at 03:46
  • hi @MorrisonChang i have move my codes to the `onProgressUpdate(String... params)` , but the `List params = new ArrayList();` does not allow me to use the "params", I must rename it. so the textbox displayed is empty.. – user1850936 Mar 31 '14 at 04:07
  • Graphic cuts off the most important part of the Java stacktrace. – avgvstvs Mar 31 '14 at 04:20
  • @avgvstvs can u pls give me some hints to solve that? I have encountered problem while rewrite the codes in `PublishProgress()` and `onProgressUpdate()` – user1850936 Mar 31 '14 at 04:39
  • If, instead of a screenshot, you could drop in the actual text of the stacktrace, I might be able to help, but looking at the image, the most important part of that stacktrace is not readable. – avgvstvs Mar 31 '14 at 04:40

1 Answers1

0

Remove the runOnUiThread(new Runnable() { public void run() { )} from doInBackground, the move the update UI codes into onPostExecute(List<String> result)

class GetMemoDetails extends AsyncTask<String, String, List<String>> {
    @Override
    protected List<String> doInBackground(String... arg0) {
        // TODO Auto-generated method stub
         int success;
         List<String> list=new ArrayList<String>();
         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);                     
                 list.add(memo.getString(TAG_MEMO_NAME));
                 list.add(memo.getString(TAG_MEMO_DETAILS));
             }else{
                 // memo with pid not found
             }
         } catch (JSONException e) {
             e.printStackTrace();
         }
        return list;
    }
    @Override
    protected void onPostExecute(List<String> result) {
          editMemoName.setText(result.get(0));
          editMemoDetails.setText(result.get(1));
    }
     @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();
        }
}
user1850936
  • 129
  • 3
  • 18