即使在其中有值,String也会向httppost发送null。我已经在互联网上查了一下,但我仍然无法找到解决方案。
这是我获取数据的方式
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();
这是我的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
正如您在LogCat中看到的那样,String变量中包含值,但它仍然向nameValuePairs
发送null。我不知道我的错误在哪里。如果有人可以帮助我,我将非常感激。非常感谢您的帮助。抱歉英语不好。
好的,这是我更新的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)
答案 0 :(得分:0)
在代码中看不到多少错误,而不是将 e.getMessage()写入LogCat:
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}
如果使用,您可能会获得更有意义的追踪:
} catch (Exception e) {
e.printStackTrace();
}