首页 \ 问答 \ Android / PHP - 努力发布数据(Android / PHP - Struggling to Post Data)

Android / PHP - 努力发布数据(Android / PHP - Struggling to Post Data)

我正在努力找出问题所在。 完全相同的代码适用于另一个项目。 如果我将它指向上一个项目中的URL,它可以在当前项目中工作 - 但是在新URL上它不会发布NameValuePairs。

这是我用来检索字符串的方法:

public static String getJSON(String url, List<BasicNameValuePair> params, int timeout) {

    String response = "";
    try {

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);

        URL uri = new URL(url);
        HttpURLConnection request = (HttpURLConnection) uri.openConnection();

        request.setUseCaches(false);
        request.setDoOutput(true);
        request.setDoInput(true);

        request.setRequestMethod("POST");
        OutputStream post = request.getOutputStream();
        entity.writeTo(post);
        post.flush();

        BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String inputLine = "";
        while ((inputLine = in.readLine()) != null) {
            response += inputLine;
            // res = response;
        }
        post.close();
        in.close();
    } catch (Exception e) {
        Log.e("Your app", "error", e);
    }

    return response;

}

这是我的PHP文件:

<?php

if (isset($_POST['Type'])){
    print_r($_POST);
}else {
    echo "No post received";
}

?>

这是我的要求:

            List<BasicNameValuePair> nameValuePair = new ArrayList<BasicNameValuePair>(1);
            nameValuePair.add(new BasicNameValuePair("Type", "20"));
            nameValuePair.add(new BasicNameValuePair("version", "1.3"));

            String stringArrayElement = "\n";
            String result = Config.getJSON(Config.MyUrl + "test4.php", nameValuePair, 1000);
            JSONObject JSON_DATA;

            // Making HTTP Request

            try {
                JSON_DATA = new JSONObject(result);

                Downloaded_menu_array = JSON_DATA.getJSONArray("Menu");

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

            }

它总是在我的PHP文件中点击“没有收到帖子”。 是否有任何与代码跳出来或我错过了别的东西?


I'm struggling to find out what the problem is. The exact same code works in another project. It works in the current project if I point it to the URL in the previous project - but on a new URL it simply won't post NameValuePairs.

This is what I'm using to retrieve the string:

public static String getJSON(String url, List<BasicNameValuePair> params, int timeout) {

    String response = "";
    try {

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);

        URL uri = new URL(url);
        HttpURLConnection request = (HttpURLConnection) uri.openConnection();

        request.setUseCaches(false);
        request.setDoOutput(true);
        request.setDoInput(true);

        request.setRequestMethod("POST");
        OutputStream post = request.getOutputStream();
        entity.writeTo(post);
        post.flush();

        BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String inputLine = "";
        while ((inputLine = in.readLine()) != null) {
            response += inputLine;
            // res = response;
        }
        post.close();
        in.close();
    } catch (Exception e) {
        Log.e("Your app", "error", e);
    }

    return response;

}

This is my PHP file:

<?php

if (isset($_POST['Type'])){
    print_r($_POST);
}else {
    echo "No post received";
}

?>

Here is my request:

            List<BasicNameValuePair> nameValuePair = new ArrayList<BasicNameValuePair>(1);
            nameValuePair.add(new BasicNameValuePair("Type", "20"));
            nameValuePair.add(new BasicNameValuePair("version", "1.3"));

            String stringArrayElement = "\n";
            String result = Config.getJSON(Config.MyUrl + "test4.php", nameValuePair, 1000);
            JSONObject JSON_DATA;

            // Making HTTP Request

            try {
                JSON_DATA = new JSONObject(result);

                Downloaded_menu_array = JSON_DATA.getJSONArray("Menu");

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

            }

It always hits "No post received" in my PHP file. Is there anything that jumps out with the code or am I missing something else?


原文:https://stackoverflow.com/questions/30463619
更新时间:2022-06-27 09:06

最满意答案

TimeractionPerformed为实际工作创建一个SwingWorker


Have the actionPerformed of the Timer create a SwingWorker for the actual work.

相关问答

更多
  • 您正在阻止UI线程,直到某些非UI工作完成。 不要那样做。 让UI线程做UI工作,安排在另一个线程中完成非UI工作,然后让它无所事事; 它将回到处理其他UI事件。 BackgroundWorker专门用于让UI执行一些非UI工作,并使用进度或结果更新UI。 让BGW的DoWork方法完成您的实际工作,更新更新的处理程序中的进度条,并在完成的处理程序中显示结果。 BGW将负责在UI线程中运行除DoWork事件之外的所有事件,因此您无需手动调用UI线程。 您的代码无法正常工作的原因是因为您通过等待任务来阻止UI ...
  • 这不应该与Swing定时器有关,这是你的代码中的其他东西。 您需要调试应用程序以查看导致延迟的原因。 That should not be related to the Swing timers, that is something else in your code. You need to debug the application to see what is causing the delay.
  • 听起来第三方库可能在EDT上做了大量的非ui工作。 如果是这样,除了要求供应商修复它,或者它们不会/不能,摆脱库并以尊重EDT的方式重新实现它之外,你可以做的事情并不多。 It sounds like the third-party library may be doing a large amount of non-ui work on the EDT. If so, there's not much that you can do about it other than either asking th ...
  • 让Timer的actionPerformed为实际工作创建一个SwingWorker 。 Have the actionPerformed of the Timer create a SwingWorker for the actual work.
  • 尽管过去广泛使用定时器会更难理解,但我建议您转向使用Java的执行程序服务。 如果您想了解计时器和执行器服务之间的一些差异,以下是一个很好的总结: Java定时器与ExecutorService? 特别是在处理多线程的游戏中,执行程序服务是不可或缺的。 Although it is more difficult to understand if you've used timers extensively in the past, I recommend that you move to using Jav ...
  • 我强烈建议将耗时的任务转移到另一个线程。 这样您就可以毫不拖延地更新UI线程。 查看这个SwingWorker示例文章 ,详细说明了为什么要使用其他线程以及如何使用它。 希望我帮忙! I would strongly suggest to move your time consuming task to another thread. This way you could update your UI thread without any delay. Check this SwingWorker exam ...
  • 您可以从TimerFrame获得一些想法。 另请参阅在Swing应用程序中使用计时器 。 You might get some ideas from TimerFrame. See also Using Timers in Swing Applications.
  • 我建议您使用两个计时器和scheduleAtFixedRate方法而不是schedule方法。 此方法的工作原理如下scheduleAtFixedRate(timerTask, delay, period) 其中: TimerTask :要执行的任务TimerTask类的任务。 在这里你可以在其中一个计时器任务中打开你的标志,然后在另一个计时器任务中关闭它。 延迟 :第一次运行任务之前的延迟量。 period :计时器任务连续执行前的占空比周期。 诀窍是安排两个具有相同延迟周期的定时器,但其中一个定时器以0 ...
  • 你需要做两件事: 也设置初始延迟,否则计时器会一直等待最初指定的时间。 重新启动计时器。 你可以像这样设置一个非常低的延迟: timer.setInitialDelay(5); timer.setDelay(5); timer.restart(); You need to do two things: Set the initial delay too, otherwise the timer will keep waiting as much as initially speci ...
  • 我已经尝试过使用Timers,但是我不确定如何正确地格式化它以便它只在每次changeLabel()内部的循环递增时暂停 使用Timer ,不要使用循环。 Timer是你启动定时器并继续执行直到你停止Timer 。 您也不制作方法,只要Timer触发,您就可以调用Action 。 因此,您需要在类中使用一个实例变量来跟踪Timer触发的次数(让我们称之为“timerCounter”)。 然后,您需要创建一个Action以便每次触发Timer调用。 所以你创建了几个实例变量: int timerCounter ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)