質問:
問題は、これが goDaddy がホストするオンライン データベースに投稿されないことです。だから私の質問はなぜですか、そしてそれを投稿するように修正するにはどうすればよいですか?
問題: PHP ページは、渡された名前と値のペアを受け取りません。
編集:
HttpURLConnection を使用するように提案されたコードを修正しました... fbid を取得しないという問題に絞り込みました。
お分かりのように、私はここで多くの宿題をしました... これが、postthread クラスに設定されている内容のログキャットです。投稿を行う私のクラスは次のとおりです。
07-02 16:41:45.108: I/PROJECTCARUSO(12308): response: {"posts":[null]}
HttpPostThread:
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();
}