- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题:
所以问题是这不会发布到由 goDaddy 托管的在线数据库。所以我的问题是为什么,以及如何修复它以将其发布到它?
问题:php 页面未接收传递给它的名称值对。
编辑:
修改了建议使用 HttpURLConnection 的代码...我已将问题缩小到无法检索 fbid。
如您所知,我在这里做了很多功课...这是我在 postthread 类中设置的内容的 logcat 这是我执行帖子的类:
07-02 16:41:45.108: I/PROJECTCARUSO(12308): response: {"posts":[null]}
HttpPost线程:
public class HttpPostThread extends Thread {
public static final int FAILURE = 0;
public static final int SUCCESS = 1;
private URL url;
ArrayList<NameValuePair> pairs;
public HttpPostThread(URL sERVICE_URL, ArrayList<NameValuePair> pairs, final Handler handler)
{
Log.i("PROJECTCARUSO", "Posting to URL: " + sERVICE_URL);
this.url =sERVICE_URL;
this.pairs = pairs;
if(pairs==null){
Log.i("PROJECTCARUSO", "URL parms were null");
this.pairs = new ArrayList<NameValuePair>();
}
}
@Override
public void run()
{
try {
HttpURLConnection conn;
String param="";
for (NameValuePair nvp : pairs) {
//you need to encode ONLY the values of the parameters
if (param == "") {
param=nvp.getName() + "=" + URLEncoder.encode(nvp.getValue(),"UTF-8");
} else {
param+= "&" + nvp.getName() + "=" + URLEncoder.encode(nvp.getValue(),"UTF-8");
}
}
Log.i("PROJECTCARUSO", "param: " + param.toString());
// Create connection
conn=(HttpURLConnection)url.openConnection();
//set the output to true, indicating you are outputting(uploading) POST data
conn.setDoOutput(true);
//once you set the output to true, you don't really need to set the request method to post, but I'm doing it anyway
conn.setRequestMethod("POST");
//Android documentation suggested that you set the length of the data you are sending to the server, BUT
// do NOT specify this length in the header by using conn.setRequestProperty("Content-Length", length);
//use this instead.
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//send the POST out
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
//build the string to store the response text from the server
String response= "";
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
}
//catch some error
catch(MalformedURLException ex){
Log.i("PROJECTCARUSO", ex.toString());
}
// and some more
catch(IOException ex){
Log.i("PROJECTCARUSO", ex.toString());
}
}
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}
这是 php:
<?php
//Make connection
$con = mysqli_connect('...','...','...') ;
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//change db to andriodnfp db
mysqli_select_db($con, 'andriodnfp');
$table= 'USER';
$id=0;
$fbid = htmlspecialchars($_GET["fbid"]);
$social = htmlspecialchars($_GET["social"]);
$name = htmlspecialchars($_GET["name"]);
$name = !empty($name) ? "'$name'" : "NULL";
$fname = htmlspecialchars($_GET["fname"]);
$fname = !empty($fname) ? "'$fname'" : "NULL";
$username = htmlspecialchars($_GET["username"]);
$username = !empty($username) ? "'$username'" : "NULL";
$email = htmlspecialchars($_GET["email"]);
$email = !empty($email) ? "'$email'" : "NULL";
$picture = htmlspecialchars($_GET["picture"]);
$picture = !empty($picture) ? "'$picture'" : "NULL";
$other = htmlspecialchars($_GET["other"]);
$other = !empty($other) ? "'$other'" : "NULL";
if (!$fbid == '') {
if (!mysqli_query($con, 'INSERT INTO '.$table.' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("'.$fbid.'","'.$social.'","'.$name.'","'.$fname.'","'.$username.'","'.$email.'","'.$picture.'","'.$other.'")')) {
printf("Errormessage: %s\n", mysqli_error($con));
die();
} else {
$posts = array('auto_increment_id'=>mysqli_insert_id($con));
};
} else {
printf("Errormessage: %s\n", "Facebook ID was null");
printf("Errormessage: %s\n", $fbid );
printf("Errormessage: %s\n", $social);
printf("Errormessage: %s\n", $name);
printf("Errormessage: %s\n", $fname);
printf("Errormessage: %s\n", $username);
printf("Errormessage: %s\n", $email);
printf("Errormessage: %s\n", $picture);
printf("Errormessage: %s\n", $other);
die();
}
mysqli_close($con);
//$posts = array($json);
$posts = array($posts);
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
?>
Apache 日志:
166.147.72.174 - - [19/Jun/2013:16:47:41 -0700] "POST ...post.php? HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 195224
166.147.72.174 - - [19/Jun/2013:16:49:08 -0700] "POST ...post.php? HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 13848
166.147.72.174 - - [19/Jun/2013:16:50:57 -0700] "POST ...post.php? HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 17899
166.147.72.174 - - [19/Jun/2013:16:52:14 -0700] "POST ...post.php HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 12514
166.147.72.174 - - [19/Jun/2013:16:53:35 -0700] "POST ...post.php HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 15190
166.147.72.174 - - [19/Jun/2013:16:54:56 -0700] "POST ...post.php HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 14373
166.147.72.174 - - [19/Jun/2013:16:56:50 -0700] "POST ...post.php HTTP/1.1" 200 155 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)" 0 "x-httpd-php5-3" "/var/chroot/home/content/31/9124131/html/android/com.projectcaruso/naturalfamilyplaning/post.php" 12017
编辑:
我什至试过java:
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (NameValuePair nvp : pairs) {
//you need to encode ONLY the values of the parameters
params.add(new BasicNameValuePair(nvp.getName(), nvp.getValue()));
Log.i("PROJECTCARUSO", "NVP: " + nvp.getName() + " - " + nvp.getValue());
}
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.close();
os.close();
conn.connect();
//build the string to store the response text from the server
String response= "";
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最佳答案
你必须在任何地方都使用 $_POST
而不是 $_GET
。
关于Android 设备上的 PHP HTTP POST 使用 HttpURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17203356/
我一直在研究一个调用 Rest API 的 portlet。当API被调用且请求的数据不存在时,返回相应的JSON格式的错误信息(带有Bad request http code - 400),如果id
我有这个在 Windows VM 上运行的简单代码: URL url = new URL("http:xxx.xxx.xxx.xxx/api/cities.json"); HttpURLConnect
我有一个这样的代码: HttpURLConnection connection = (HttpURLConnection) loginUrl.openConnection(); connection.
Google 提供了 2 个不同的 HttpURLConnection 用法示例。 调用InputStream的close http://developer.android.com/training/
我想对自己编写的HTTP Servlet发出POST请求。通过使用URL.openConnection()方法,好的情况(HTTP响应代码200)始终可以正常工作。但是当我收到所需的错误响应代码(例如
您好,我已经开始使用 HttpUrlConnection,我有一个关于 Http 请求实际发送时间的问题。 我在某处读到,调用 getInputStream() 时会发送实际请求。不过,我已经编写了一
从 android 创建 HTTPURLConnection 时,有人对何时关闭连接和何时断开连接有任何经验吗?是否应该始终使用断开连接,以便可以从池中重用连接而不是重新创建连接?使用断开连接与关闭有
问题: 尝试“连接”时出现间歇性但频繁的故障。 代码、调试消息和堆栈跟踪如下。我看过类似的 问题解答,以及 android 开发人员帮助,但找不到 对失败的回答。 任何帮助表示赞赏。 编码: publ
我一直在尝试创建一个小的应用程序,该应用程序连接到editText中的url并返回状态代码。 这是onCreate代码: override fun onCreate(savedInstanceSta
我正在尝试创建 get 连接,但是当我读取服务器上的 header 时,我发现请求方法接收为 POST 而不是 GET 最佳答案 尝试删除此行:connection.setDoOutput(true)
我正在尝试将 x-www-form-urlencoded 格式的正文发送到我的本地 Web API。我可以毫无问题地进行 GET 。这是我所做的: String urlParameters =
我是 Android 开发新手,在互联网上进行了一些咨询后,我想出了以下代码来连接到 Url、POST 字符串内容并读取其响应。 private static java.lang.String get
我使用 HTTPUrlConnection 和 GET 方法从服务器获取数据,返回字符串大约 13k 个字符,但我只收到 1228 个字符。 有什么解决方案可以超过接收所有字符吗? 这是我的代码: U
我正在使用 HttpUrlConnection 来使用 REST 服务。当我执行 GET 时,就像下面显示的那样,我不想获取逐个字符返回的信息。我想以返回的格式获得结果。例如,在这里,我想获得一个 b
我从互联网上找到了下面的两段代码,我正在我的应用程序中使用它。 我真的不明白的一件事是,为什么没有调用 HttpUrlConnection.connect() 来建立 Http 连接(握手),并且没有
当使用下面的代码发出 get 请求时: private String get(String inurl, Map headers, boolean followredirects) throws Ma
我向我的用户提供下载文件的机会。 有时这个文件非常大,我在下载时实现一个 ProgressDialog。 好吧,如果用户点击返回,我想取消AsyncTask并关闭连接: @Ov
我正在尝试使用 Java 建立与立交桥的服务器连接。但是,当我的请求参数中有空格(例如 Bulach West)时,我总是会收到错误的请求响应。我使用的代码如下: StringBuilder cont
我使用 HttpURLConnection 按顺序向服务器 HttpServer 发出多个短请求。我期望的最大吞吐量是多少?我无法获得超过 25 条记录/秒的数据。 我需要每秒至少 5000 条记录。
我使用以下连接来恢复下载: connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); co
我是一名优秀的程序员,十分优秀!