【问题标题】:String variable sends null to httppost字符串变量将 null 发送到 httppost
【发布时间】:2015-04-18 08:56:42
【问题描述】:

String 将 null 发送到 httppost,即使其中有值。我已经在互联网上查找了,但我仍然无法找到解决方案。

这就是我获取数据的方式

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

            }
        });

这就是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());
    }
}

这是我的 php 文件

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();

这是我的日志猫

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

正如您在 LogCat 中看到的,String 变量中有值,但它仍然将 null 发送到 nameValuePairs。我不知道我的错误在哪里。如果有人可以帮助我,我将不胜感激。非常感谢您的帮助。抱歉英语不好。

好的,这是我更新的 logcat 我已经找到了解决方案

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)

【问题讨论】:

  • 哪个变量?你有这么多。
  • String variable sends null 你怎么知道的?
  • 在 logcat @njzk2

标签: android


【解决方案1】:

在您的代码中看不到太多错误,而是将 e.getMessage() 写入 LogCat :

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

如果使用,您可能会获得更有意义的跟踪:

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

【讨论】:

  • 天啊..我从来没想过这个大声笑..谢谢人,当我看到日志时,我在这里找到了解决方案..link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
相关资源
最近更新 更多