java - 从文本框传递参数

标签 java android http url

基于这篇文章(http://theopentutorials.com/tutorials/android/http/android-how-to-send-http-get-request-to-servlet-using-apache-http-client/),做了一个简单的http客户端。但我无法向 url 添加参数。

我正在尝试从文本框传递参数:

EditText editTextValue
public String givenValue = editTextValue.getText (). toString ();
public final String URL = "http://example.com/script.php?ip =" + givenValue;

但出现此错误:

ERROR / AndroidRuntime (7103): FATAL EXCEPTION: main
at com.example.MainActivity. (MainActivity.java: 39) (public String givenValue = editTextValue.getText (). toString () ;)

如何修复它?

抱歉我的英语不好。

更新:

完整来源:

package com.example.App;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.widget.EditText;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    Button btnGetInfo;
    TextView textView;
    EditText editTextValue;

    public String givenValue = editTextValue.getText().toString();

    public String URL = "http://example.com/script.php?value=" + givenValue;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      findViewsById();

        btnGetInfo.setOnClickListener(this);
    }

    private void findViewsById() {
        btnGetInfo = (Button) findViewById(R.id.buttonGetInfo);
        textView = (TextView) findViewById(R.id.textView);
        editTextValue = (EditText) findViewById(R.id.editTextValue);
    }

    public void onClick(View view) {

            GetXMLTask task = new GetXMLTask();
            task.execute(new String[] {URL});
    }

    private class GetXMLTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String output = null;
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }
            return output;
        }

        private String getOutputFromUrl(String url) {
            String output = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                output = EntityUtils.toString(httpEntity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return output;
        }

        @Override
        protected void onPostExecute(String output) {
            textView.setText(output);
        }
    }
}

并记录:

ERROR/AndroidRuntime(8367): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.App/com.example.App.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.example.App.MainActivity.<init>(MainActivity.java:28)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    ... 11 more

最佳答案

我认为您的 EditText 可能为空。 您在调用 getText() 之前是否设置了 edittext? 尝试:

EditText editTextValue = (EditText) findViewByItd(R.id.yourEditText);

根据教程,您可以将此行放在findViewsById中。

private void findViewsById() {
    button = (Button) findViewById(R.id.button);
    outputText = (TextView) findViewById(R.id.outputTxt);
    editTextValue = (EditText) findViewById(R.id.editTextValue);
}

编辑: 你必须移动

public String givenValue = editTextValue.getText().toString();

到适当的地方。

editTextValue.getText() 不应在 onCreate 和 findViewById 之前调用。

也许您可以尝试将该行放在 onPostExecute 或 onClick 中。 (仅供测试)

更新2:

将给定值和 URL 移至 onClick

注意:我尚未测试以下代码:

package com.example.App;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.widget.EditText;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    Button btnGetInfo;
    TextView textView;
    EditText editTextValue;

    public String givenValue;

    public String URL;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      findViewsById();

        btnGetInfo.setOnClickListener(this);
    }

    private void findViewsById() {
        btnGetInfo = (Button) findViewById(R.id.buttonGetInfo);
        textView = (TextView) findViewById(R.id.textView);
        editTextValue = (EditText) findViewById(R.id.editTextValue);
    }

    public void onClick(View view) {
            givenValue = editTextValue.getText().toString();
            URL = "http://example.com/script.php?value=" + givenValue;
            GetXMLTask task = new GetXMLTask();
            task.execute(new String[] {URL});
    }

    private class GetXMLTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String output = null;
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }
            return output;
        }

        private String getOutputFromUrl(String url) {
            String output = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                output = EntityUtils.toString(httpEntity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return output;
        }

        @Override
        protected void onPostExecute(String output) {
            textView.setText(output);
        }
    }
}

关于java - 从文本框传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18964854/

相关文章:

javascript - NodeJS res.on() 未触发。 res.on() 不是函数

java - 循环算法中的康威生命游戏

java - 字符串标记器的奇怪行为

java - Hibernate 和 DB2- 在表中插入或删除时发出错误 : util. JDBCExceptionReporter

android - 如何在自定义 View 上显示数字键盘

node.js - 在 Node JS 中发送低级原始 HTTP/HTTPS 请求

java - React Native : java. util.zip.ZipException : duplicate entry: com/google/android/gms/iid/zzc. 类

Android SQLite 搜索 : NullPointerException?

android - 如何将 View 设置为 recyclerview 中的最后一项

http - 在 HTTPS 上显示的本地时间码