将数据从Android应用程序发布到PHP网站的问题

时间:2014-07-18 20:32:25

标签: php android

我开发了一个Android应用程序,用于将数据发布到PHP网站。

一切都很好看。它必须将数据发布到http://tayyab001.base.pk/kami.php,但它无效。没有错误,没有问题,但它没有工作。

logcat显示一切正常。

问题出在哪里?

package com.latlongapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

    Button sendButton;

    EditText msgTextField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
    }

    class SendTask extends AsyncTask<String, Void, String> {
        @Override
            protected String doInBackground(String... params) {
                  // get the message from the message text box
                String msg = msgTextField.getText().toString();  

                // make sure the fields are not empty
                if (msg.length()>0)
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
                 try {
                   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                   nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                   nameValuePairs.add(new BasicNameValuePair("message", msg));
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   httpclient.execute(httppost);
                   msgTextField.setText(""); // clear text box
                 } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                 }

                }
                else
                {
                    // display message if text fields are empty
                    Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
                }
             return null;
        }}

     public void send(View v)
        {
         new SendTask().execute();
        }

}

正在使用的PHP SCript是

<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"]; 
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
    android:text="Message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    /> 

<EditText
    android:id="@+id/msgTextField"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:text="Send"
    android:id="@+id/sendButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="send"
    /> 

manifest

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />


        <uses-permission android:name="android.permission.INTERNET" /> 



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.latlongapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

首先,您应该从doInBackground()删除对UI线程的所有调用。

但主要的错误是你写了#34; http&#34;创建HttpPost对象时两次。

试试这段代码。它的工作原理是因为我刚用它发布了你网站的答案:)

public class MainActivity extends Activity 
{   
    Button sendButton;

    EditText msgTextField;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);

        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
    }

    class SendTask extends AsyncTask<String, Void, String> 
    {
        String msg = "";

        @Override
        protected void onPreExecute() 
        {
            msg = msgTextField.getText().toString();
        }

        @Override
        protected String doInBackground(String... params) 
        {   
            // make sure the fields are not empty
            if (msg.length() > 0)
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://tayyab001.base.pk/kami.php");

                 try 
                 {
                     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                     nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                     nameValuePairs.add(new BasicNameValuePair("message", msg));

                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     httpclient.execute(httppost);                         
                 } 
                 catch (ClientProtocolException e) 
                 {
                     Log.d("MainActivityTest", "ClientProtocolException caught.");
                 } 
                 catch (IOException e) 
                 {
                     Log.d("MainActivityTest", "IOException caught. " + e.toString());
                 }
            }
            else
            {
                // display message if text fields are empty
                Log.d("MainActivityTest", "All fields are required.");
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) 
        {
            msgTextField.setText("");
        }           
    }

     public void send(View v)
     {
         new SendTask().execute();
     }
}