Android HttpPost无法正常工作

时间:2014-09-22 19:50:43

标签: php android mysql json http-post

我在localhost上有一个mysql数据库:88名为' test'使用用户名' root'这个表叫做'例子'在那里。 我试图使用PHP脚本和方法$ _POST在我的Android应用程序上获取数据。但它只是没有正常工作,我无法理解为什么。这是我的java代码:

public class MainActivity extends Activity implements View.OnClickListener
{
    TextView txt;
    EditText editText;
    ProgressBar pb;
    Button button;

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

        txt = (TextView) findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);
        pb = (ProgressBar) findViewById(R.id.progressBar);
        button = (Button) findViewById(R.id.button);

        pb.setVisibility(View.GONE);
        button.setOnClickListener(this);
    }

    public void onClick(View v)
    {
        if(editText.getText().toString().length()<1)
            Toast.makeText(this, "Enter something!", Toast.LENGTH_SHORT);
        else{
            pb.setVisibility(View.VISIBLE);
            new sendData().execute(editText.getText().toString());
        }
    }

    class sendData extends AsyncTask<String, Integer, Double>
    {
        @Override
        protected Double doInBackground(String... strings) {
            postData(strings[0]);
            return null;
        }

        @Override
        protected void onPostExecute(Double aDouble) {
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            pb.setProgress(values[0]);
        }

        public void postData(String valueIWantToSend)
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/basicphp/setData.php");
            httpPost.addHeader("content-type", "application/json");
            try
            {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("name", valueIWantToSend));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);
                Log.d("postData","response" + response.getEntity());
            }
            catch(IOException e){
                Log.wtf("postData", e.getMessage());
            }
        }
    }

这是我的php脚本:

<?php
    $con = mysqli_connect('127.0.0.1:3306', 'root', '', 'test');    

    if(mysqli_connect_errno())
        echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $name = mysqli_real_escape_string($con, $_POST['name']);

    $sql =  
    "INSERT INTO `example` (name) VALUES ("$name");";

    mysqli_query($con, $sql) or die('Error occured: ' . mysqli_error($con));

    echo 'Values inserted successfully!';

    mysqli_close($con);
?>

logcat的:

09-22 19:25:16.916    2170-2170/my.com.jsontry D/dalvikvm﹕ Late-enabling CheckJNI
09-22 19:25:17.020    2170-2170/my.com.jsontry E/Trace﹕ error opening trace file: No such file or directory (2)
09-22 19:25:17.112    2170-2170/my.com.jsontry D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
09-22 19:25:17.116    2170-2170/my.com.jsontry D/﹕ HostConnection::get() New Host Connection established 0xb9620558, tid 2170
09-22 19:25:17.132    2170-2170/my.com.jsontry D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-22 19:25:17.132    2170-2170/my.com.jsontry D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
09-22 19:25:17.220    2170-2170/my.com.jsontry W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-22 19:25:17.256    2170-2170/my.com.jsontry D/OpenGLRenderer﹕ Enabling debug mode 0
09-22 19:25:24.400    2170-2170/my.com.jsontry W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-22 19:26:28.272    2170-2188/my.com.jsontry A/postData﹕ Connection to http://10.0.2.2 refused
09-22 19:26:28.292    2170-2173/my.com.jsontry D/dalvikvm﹕ GC_CONCURRENT freed 111K, 9% free 2599K/2852K, paused 10ms+2ms, total 20ms

显然,我做错了什么,请帮助我理解究竟是什么,纠正错误。我已经搜索过类似的问题,但仍然没有找到答案。如果你能用或多或少的简单单词解释所有内容,我真的很感激,因为我在android和php编程方面都是一个新手。

1 个答案:

答案 0 :(得分:0)

  

我在localhost上有一个mysql数据库:88

然后

HttpPost httpPost = new HttpPost("http://10.0.2.2/basicphp/setData.php");

您没有在任何地方指定端口......

所以:

Connection to http://10.0.2.2 refused

是因为你正在尝试错误的端口,默认情况下它将是80而不是88。