如何将数据从Android应用程序发布到wamp服务器?

时间:2015-12-03 06:19:47

标签: android server http-post wamp

我正在尝试使用 http请求方法将数据从Android应用程序发布到Wamp服务器。

当我运行应用程序时,Android应用程序上会显示成功消息,但Wamp服务器的表中没有插入数据。 logcat上没有显示错误。我做错了什么?

public class MainActivity extends Activity {
    private EditText editTextName;
    private EditText editTextAdd;@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextAdd = (EditText) findViewById(R.id.editTextAddress);

}

public void insert(View view) {
    String name = editTextName.getText().toString();
    String add = editTextAdd.getText().toString();
    insertToDatabase(name, add);

}

private void insertToDatabase(String name, String add) {
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String paramUsername = params[0];
            String paramAddress = params[1];
            String name = editTextName.getText().toString();
            String add = editTextAdd.getText().toString();
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("address", add));
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://ip address of my system/Employee3/create_product.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }
            return "success";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
            TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
            textViewResult.setText("Inserted");
        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(name, add);}
}

1 个答案:

答案 0 :(得分:0)

对于Http调试,我建议你首先使用一些工具来测试界面,比如curl或postMan。确保post / get方法在这些工具中正常工作,然后在Android中测试界面