如何将用户输入(Edittext)传递给服务器上的php脚本?我试图通过wamp在本地工作。我已经允许wamp conf文件中的所有连接,并在我的www文件夹中创建了我正在使用的文件夹的别名。我的代码如下:
public class GetItemsActivity extends FoodSearchActivity {
public void postData(View v) {
//get food from edit text box
String food = txtFoodSearch.getText().toString();
System.out.println(food); //see if user input is being picked up
if(food.length()>0) {
// Create a new HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("000.000.000.00/SlimandSave/test.php"); //use address of local web server
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Food", "food"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
txtFoodSearch.setText(" "); //reset the message text field
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
} else {
//display message if text field is empty
Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
}
}
}
现在我只想发布数据,所以我的php只是将搜索文本输出到文本文件;
file_put_contents("myfile.txt", print_r( $_POST, true ));
?>
我一直在看这里的一些问题,我现在不确定我是否应该使用asynctask,或者我是否遗漏了一些明显的东西。也许这是PHP?我感谢任何提示/建议。
编辑:我刚刚发现,如果我将编辑文本字段留空,那么当我点击搜索时应用仍会崩溃,所以不确定现在在哪里查看
进一步编辑:我刚刚发现我从未将活动添加到按钮单击的清单中,因此应用程序不再挂起并在按钮单击后崩溃。
我现在从日志中得到这个,
02-13 15:39:16.826: E/EditText(26019): bread
02-13 15:39:16.876: E/AndroidRuntime(26019): FATAL EXCEPTION: main
02-13 15:39:16.876: E/AndroidRuntime(26019): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.college.slimandsave/com.college.slimandsave.GetItemsActivity}: java.lang.ClassCastException: com.college.slimandsave.GetItemsActivity cannot be cast to android.app.Activity
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.os.Looper.loop(Looper.java:137)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-13 15:39:16.876: E/AndroidRuntime(26019): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 15:39:16.876: E/AndroidRuntime(26019): at java.lang.reflect.Method.invoke(Method.java:511)
02-13 15:39:16.876: E/AndroidRuntime(26019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-13 15:39:16.876: E/AndroidRuntime(26019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-13 15:39:16.876: E/AndroidRuntime(26019): at dalvik.system.NativeStart.main(Native Method)
02-13 15:39:16.876: E/AndroidRuntime(26019): Caused by: java.lang.ClassCastException: com.college.slimandsave.GetItemsActivity cannot be cast to android.app.Activity
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
02-13 15:39:16.876: E/AndroidRuntime(26019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
02-13 15:39:16.876: E/AndroidRuntime(26019): ... 11 more
最终让它发挥作用。在上面的代码中,我不得不使用txtFoodSearch.getText()。toString()而不仅仅是食物,我只是使用我的wamp服务器的ip,我需要将http://添加到ip地址,谢谢你建议。
答案 0 :(得分:1)
我是否应该使用
asynctask
,或者我是否遗漏了明显的东西。也许这是PHP?
当然你应该使用它,因为main Thread
上不允许进行网络操作。
也许这是php?我感谢任何提示/建议。
以下是我的建议:
您可以按照以下方式执行操作:
private class SearchFoodTask extends AsyncTask<Void, Void, Void> {
protected void doInBackground(Void... params) {
//here you execute the post request
// You can call postData() here
}
protected void onPostExecute() {
//onPostExecute is called when background task executed
//this is the place to update the interface
txtFoodSearch.setText(" ");
}
}
使用logcat,将内容记录到屏幕或控制台,您必须知道哪里有问题。也不要只捕获exeption,你必须使用它们将堆栈跟踪打印到logcat,否则它是没有意义的。
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
保持简单我喜欢你写的文件。我个人喜欢使用error_log()
,即使它可能是出于其他目的。您可以通过添加更多检查来改进脚本:
<?php
// use file_put_contents, fwrite or error_log whichever you prefer
//If this gets written to the log file it means
//Android was able to reach
error_log("script got called from");
if(isset($_POST)){
error_log("Post value is set".var_dump($_POST));
if(isset($_POST['Food'])){
error_log("Post food value is set".$_POST['Food']);
}else{
error_log("Post food value is not set");
}
}else{
error_log("Post array not set");
}
?>
如果没有记录任何内容,则很可能是问题是应用程序。
如果您遵循这些程序,您永远不必怀疑Maybe it's the php?
您将确切地知道问题所在。