httpclient.execute(httppost)不起作用

时间:2015-02-17 01:02:31

标签: java php android mysql

我正在尝试使用php文件从我的Android应用程序中插入mysql数据库中的数据。我不知道我做错了什么,这是我的android代码:

cargar_preguntas.java

package com.example.nico.leercomentariosbdremota2;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class cargar_preguntas extends Activity {

private Spinner opciones;
private EditText preg, op1, op2, op3, op4, resp, rta;
private Button add;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cargar_pregunta);
    opciones = (Spinner) findViewById(R.id.spinner);
//Creamos el adaptador
    ArrayAdapter<CharSequence> adapter =       ArrayAdapter.createFromResource(this, R.array.Tema,    android.R.layout.simple_spinner_item);
//Añadimos el layout para el menú
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Le indicamos al spinner el adaptador a usar
    opciones.setAdapter(adapter);
    preg = (EditText) findViewById(R.id.txtPreg);
    op1 = (EditText) findViewById(R.id.txtOp1);
    op2 = (EditText) findViewById(R.id.txtOp2);
    op3 = (EditText) findViewById(R.id.txtOp3);
    op4 = (EditText) findViewById(R.id.txtOp4);
    rta = (EditText) findViewById(R.id.txtRta);
    add = (Button) findViewById((R.id.btnAgregar));
}


    public void onClickCargarPreg(View view)
    {

        add.setText("si");
        HttpClient httpclient = new DefaultHttpClient();
        add.setText("1");
        HttpPost httppost = new HttpPost("...Insertar_Preguntas.php");
        try
        {

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("Pregunta",preg.getText().toString()));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            Log.e("pass 1", "connection success ");

        }
        catch(Exception e)
        {
            e.printStackTrace();

        }


    }
}

我的Insertar_Preguntas.php?

<?php

$connect = mysqli_connect(".......");

if(mysqli_connect_errno($connect))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "success";
}


$preg = $_GET['Pregunta'];



$query = mysqli_query($connect, "insert into FT_Preguntas (Pregunta) values ('$preg') ");

mysqli_close($connect);

&GT;

由于

1 个答案:

答案 0 :(得分:0)

尼古拉斯   我想如果你替换&#34; $ _ POST [&#39; Pregunta&#39;]&#34; for&#34; $ _ GET [&#39; Pregunta&#39;]&#34;在你的PHP代码中,你将获得你想要的值。

迈克卡根