连接Android PHP / MySQL

时间:2015-04-25 18:51:34

标签: android http

尝试建立与mysql的连接时,我收到以下错误。

package com.example.projetoansc;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

public class Conexao extends AsyncTask<String, String, String> {

    List<NameValuePair> postparams= new ArrayList<NameValuePair>();
    private String url=null;


    public Conexao(String url, List<NameValuePair> params){
        this.postparams = params;
        this.url = url;
    }

    @Override
    protected String doInBackground(String... params) {

        try{
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(postparams));

            HttpResponse httpResponse = client.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            return sb.toString();

            }catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        return "";


    }
}

//And the class that class that call this object Conexao.class

public void onClick(View v) {
    String url = "http://myhost/phpandroid/Contexto.php";

    ArrayList<NameValuePair> parametros = new ArrayList<NameValuePair>();

    parametros.add(new BasicNameValuePair("nome",editText1.getText().toString()));
    parametros.add(new BasicNameValuePair("nome",editText2.getText().toString()));



    try{
        String resultado = new Conexao(url, parametros).execute().get();

        if(resultado.equals(editText1.getText().toString())){
            Log.i("logar", "resposta = "+ resultado);
        }
        else{
            Log.i("logar", "resposta = "+ resultado);
        }


    }catch(Exception e){
        Log.i("logar", "erro = " + e);
    }
}
logCat中的

错误

04-25 18:41:37.447: W/AudioTrack(347): AUDIO_OUTPUT_FLAG_FAST denied by client
04-25 18:41:37.937: I/logar(1290): resposta = <br />
04-25 18:41:37.937: I/logar(1290): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-25 18:41:37.937: I/logar(1290): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: senha in C:\wamp\www\PhpAndroid\Contexto.php on line <i>7</i></th></tr>
04-25 18:41:37.937: I/logar(1290): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
04-25 18:41:37.937: I/logar(1290): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
04-25 18:41:37.937: I/logar(1290): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>244656</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\PhpAndroid\Contexto.php' bgcolor='#eeeeec'>..\Contexto.php<b>:</b>0</td></tr>
04-25 18:41:37.937: I/logar(1290): </table></font>
04-25 18:41:37.937: I/logar(1290):     
04-25 18:41:37.937: I/logar(1290):  
04-25 18:41:37.972: I/Choreographer(1290): Skipped 160 frames!  The application may be doing too much work on its main thread.
04-25 18:49:40.900: I/UsageStatsService(347): User[0] Flushing usage stats to disk
04-25 18:59:46.709: I/ProcessStatsService(347): Prepared write state in 20ms
04-25 18:59:46.770: I/ProcessStatsService(347): Prepared write state in 4ms
04-25 18:59:46.871: I/ProcessStatsService(347): Pruning old procstats: /data/system/procstats/state-2015-04-20-17-04-33.bin
04-25 19:01:00.011: I/ActivityManager(347): Killing 757:com.android.music/u0a33 (adj 13): empty for 1802s
04-25 19:01:00.132: I/ActivityManager(347): Killing 693:com.android.printspooler/u0a39 (adj 15): empty for 1811s
04-25 19:01:46.852: I/ActivityManager(347): Killing 1011:com.android.settings/1000 (adj 13): empty for 1819s
04-25 19:01:46.976: I/ActivityManager(347): Killing 961:com.android.managedprovisioning/u0a8 (adj 15): empty for 1824s
04-25 19:01:47.044: I/ActivityManager(347): Killing 923:com.android.dialer/u0a4 (adj 15): empty for 1827s
04-25 19:02:00.016: I/ActivityManager(347): Killing 1088:com.android.exchange/u0a27 (adj 15): empty for 1810s
04-25 19:02:00.067: I/ActivityManager(347): Killing 802:android.process.media/u0a5 (adj 15): empty for 1811s
04-25 19:04:47.610: I/art(656): Background partial concurrent mark sweep GC freed 4927(338KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 1520KB/2032KB, paused 1.151ms total 383.637ms

变量resultado正在接收一个大代码html,而不是来自mysql的数据。

1 个答案:

答案 0 :(得分:0)

这是问题

 parametros.add(new BasicNameValuePair("nome",editText1.getText().toString()));
 parametros.add(new BasicNameValuePair("nome",editText2.getText().toString()));

这两个参数有“nome”,这就是为什么“senha”不会被定义为将其中一个改为“senha”