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
最满意答案
让
Timer
的actionPerformed
为实际工作创建一个SwingWorker
。Have the
actionPerformed
of theTimer
create aSwingWorker
for the actual work.
相关问答
更多-
您正在阻止UI线程,直到某些非UI工作完成。 不要那样做。 让UI线程做UI工作,安排在另一个线程中完成非UI工作,然后让它无所事事; 它将回到处理其他UI事件。 BackgroundWorker专门用于让UI执行一些非UI工作,并使用进度或结果更新UI。 让BGW的DoWork方法完成您的实际工作,更新更新的处理程序中的进度条,并在完成的处理程序中显示结果。 BGW将负责在UI线程中运行除DoWork事件之外的所有事件,因此您无需手动调用UI线程。 您的代码无法正常工作的原因是因为您通过等待任务来阻止UI ...
-
Swing定时器的问题(Issues with Swing timers)[2023-01-23]
这不应该与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 ...