HttpUrlConnection导致应用程序崩溃

时间:2017-09-25 13:35:51

标签: android httpurlconnection

我是一名初级Android开发人员,目前正在开发餐厅队列系统。我已经设置了一个回复键的PHP页面'我要调用此页面,但是当我调用该函数时,我的应用程序总是崩溃。

我已经在Android清单中添加了互联网权限。

主要活动代码是:

package com.example.mrdias.geradorsenha;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText campoNome, campoEspecial;
    TextView textoNome, textoSenha;
    Button botaoSenha;
    String nome, especial;
    String contador;
    String vazio = "";



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

        campoNome = (EditText) findViewById(R.id.campoNome);
        campoEspecial = (EditText) findViewById(R.id.campoEspecial);
        botaoSenha = (Button) findViewById(R.id.botaoSenha);
        textoNome = (TextView) findViewById(R.id.mostrarNome);
        textoSenha = (TextView) findViewById(R.id.mostrarSenha);


    }


    public void adicionarNomes (View view){
        nome = campoNome.getText().toString();
        especial = campoEspecial.getText().toString();
        textoNome.setText(nome);
        textoSenha.setText(especial);

        String method = "registro";

        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method, nome, especial);

    }

    public void gerasenha (View view){
        GeraSenha gerasenha = new GeraSenha();
        gerasenha.execute();
        contador = gerasenha.s;
    }
}

GeraSenha.java是这样的:

    import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Mr. Dias on 25/09/2017.
 */

public class GeraSenha extends AsyncTask<String, Void, String> {
    String s;
    TextView textoSenha;

    @Override
    protected void onPreExecute() {
        textoSenha.findViewById(R.id.mostrarSenha);
    }

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

        URL url = null;
        try {
            url = new URL("http://www.mrdiasfotografia.com.br/mysql/gerarsenha.php");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection mUrlConnection = null;
        try {
            mUrlConnection = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mUrlConnection.setDoInput(true);

        InputStream is = null;
        try {
            is = new BufferedInputStream(mUrlConnection.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            mUrlConnection.disconnect();
        }
        s = String.valueOf(is);
        return s;
    }


    protected void onPostExecute(String result) {
        textoSenha.setText(result);
        super.onPostExecute(s);
    }
}

所以......我修复了textview问题,但是现在我得到的是这个&#34; com.example.mrdias.geradorsenha.gerasenha@8963a1"任何想法?

2 个答案:

答案 0 :(得分:0)

on preExecute method 
textoSenha variable is null;

textoSenha.findViewById(R.id.mostrarSenha);

它将获得NullPointerException。

答案 1 :(得分:0)

替换您的代码

curl

使用以下代码

 @Override
protected void onPreExecute() {
    textoSenha.findViewById(R.id.mostrarSenha);
}