【问题标题】:Post data from edittext to php script using httppost使用 httppost 将数据从 edittext 发布到 php 脚本
【发布时间】:2014-02-07 12:18:17
【问题描述】:

如何将用户输入 (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 地址,感谢任何建议。

【问题讨论】:

  • nameValuePairs.add(new BasicNameValuePair("Food", food));
  • nameValuePairs.add(new BasicNameValuePair("Food", food));对于第一次测试,您甚至可以使用 new Thread()。
  • 谢谢伙计们,我没有发现,但是当我点击搜索时,应用程序只是挂起,然后仍然崩溃。

标签: php android post android-asynctask


【解决方案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,将事情记录到屏幕或控制台,你必须知道哪里有问题。也不要只是捕捉异常,你必须使用它们将堆栈跟踪打印到 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?,您将确切知道问题出在哪里。

【讨论】:

  • 感谢您的帮助,另一件事是当我扩展 AsyncTask 时我的 edittext 字段 (txtFoodSearch) 无法解决,但如果我扩展 FoodSearchActivity 它可以,但我无法使用 AsyncTask。我已将其宣布为公开,因此不确定为什么无法解决。
  • 也许你可以告诉我你是如何声明和初始化它的。让我知道我们会解决的。您可以尝试将其设为静态,因为它是 2 个不同的类。
  • 感谢将其声明为静态解决方案,但一如既往,当我解决一个问题时,会出现另一个问题。
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 2014-11-25
  • 1970-01-01
  • 2016-11-26
  • 2015-07-25
  • 1970-01-01
相关资源
最近更新 更多