-1

String sends null to httppost even though has value in it. I already looked up on the internet but I still wasn´t able to find the solution.

This is how I get the data

            tr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerForContextMenu(tr);
                openContextMenu(tr);
                TableRow t = (TableRow) v;
                firstTextView = (TextView) t.getChildAt(2);
                secondTextView = (TextView) t.getChildAt(4);
                thirdTextView = (TextView) t.getChildAt(5);
                fourthTextView = (TextView) t.getChildAt(7);

                strProgram = firstTextView.getText().toString();
                strShour = secondTextView.getText().toString();
                strEhour = thirdTextView.getText().toString();
                strStatus = fourthTextView.getText().toString();
                System.out.println(strProgram+" "+strShour+" "+strEhour+" "+strStatus);

            }
        });

And this is the method of httppost

public void cancelClass() {
    try{
        String p = firstTextView.getText().toString();
        String s = secondTextView.getText().toString();
        String e = thirdTextView.getText().toString();
        System.out.println(p+" "+s+" "+e+" ");

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://192.168.1.105/my_folder_inside_htdocs/cancelClass.php"); // make sure the url is correct.
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>();
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
        nameValuePairs.add(new BasicNameValuePair("strProgram",p));
        nameValuePairs.add(new BasicNameValuePair("strShour",s));
        nameValuePairs.add(new BasicNameValuePair("strEhour",e));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response);

        if(response.equalsIgnoreCase("No Such Timetable")){
            Toast.makeText(getApplicationContext(),"Database problem", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getApplicationContext(),"Timetable found", Toast.LENGTH_SHORT).show();
        }

    }catch(Exception e){
        System.out.println("Exception : " + e.getMessage());
    }
}

And this is my php file

mysql_select_db($database_localhost, $localhost);

$program = $_POST['strProgram'];
$startHour = $_POST['strShour'];
$endHour = $_POST['strEhour'];
$query_search = "select * from diptimetable where program = '".$program."' AND startHours = '".$startHour."' AND endHours = '".$endHour."'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec) and mysql_fetch_assoc($query_exec) ;
//echo $rows;
 if($rows == 0) { 
 echo "No Such Timetable"; 
 }
 else  {
    echo "Timetable found"; 
}
mysql_close();

And this is my logcat

02-17 23:25:00.883  25644-25644/com.eztimetable I/System.out﹕ DIP6A 11:00 AM 12:00 PM Have class
02-17 23:25:00.883  25644-25644/com.eztimetable I/System.out﹕ DIP6A 11:00 AM 12:00 PM
02-17 23:25:00.883  25644-25644/com.eztimetable I/System.out﹕ Exception : null

As you can see in the LogCat , the String variable has value in it, but it still sends null to nameValuePairs. I don't know where my mistake is. If someone can help me, I am very grateful. Thank you very much for your help. Sorry for the bad English.

okay this is my updated logcat and i already find the solution

02-17 23:57:10.493  27161-27161/com.eztimetable W/System.err﹕ android.os.NetworkOnMainThreadException
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at java.net.Socket.connect(Socket.java:843)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
02-17 23:57:10.513  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.eztimetable.LecturerTimetable$3.run(LecturerTimetable.java:330)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.app.Activity.runOnUiThread(Activity.java:4893)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.eztimetable.LecturerTimetable.cancelClass(LecturerTimetable.java:311)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.eztimetable.LecturerTimetable.onContextItemSelected(LecturerTimetable.java:293)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.app.Activity.onMenuItemSelected(Activity.java:2693)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3894)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:983)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.widget.AdapterView.performItemClick(AdapterView.java:299)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.widget.AbsListView$PerformClick.run(AbsListView.java:2949)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.widget.AbsListView$3.run(AbsListView.java:3683)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.os.Looper.loop(Looper.java:149)
02-17 23:57:10.523  27161-27161/com.eztimetable W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
02-17 23:57:10.533  27161-27161/com.eztimetable W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-17 23:57:10.533  27161-27161/com.eztimetable W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
02-17 23:57:10.533  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-17 23:57:10.533  27161-27161/com.eztimetable W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
02-17 23:57:10.533  27161-27161/com.eztimetable W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
SkyClasher
  • 171
  • 2
  • 11

1 Answers1

0

Cannot see much wrong in your code but instead of writing e.getMessage() into LogCat :

} catch (Exception e) {
    System.out.println("Exception : " + e.getMessage());
}

You might get more meaningful trace if use :

} catch (Exception e) {
    e.printStackTrace();
}
harism
  • 6,011
  • 1
  • 36
  • 31
  • OMG.. i never thought of this lol.. Thx man I found the solution in here when see the log .. [link](http://stackoverflow.com/questions/25093546/android-os-networkonmainthreadexception-at-android-os-strictmodeandroidblockgua) – SkyClasher Feb 17 '15 at 16:01