【问题标题】:Pass a parameter from a text box从文本框中传递参数
【发布时间】:2013-09-23 17:00:02
【问题描述】:

根据这篇文章(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

【问题讨论】:

  • 您的英语很好,但您的 logcat 可以做一些工作;) 请您发布完整的 logcat。另外,你有没有初始化你的EditText
  • 我刚开始学编程,为了不误导大家贴出整个源码和日志
  • 没问题,但是当你崩溃时总是发布完整的 logcat。看看我的回答,应该会有帮助

标签: java android http url


【解决方案1】:

我认为您的 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:

将 givenValue 和 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);
        }
    }
}

【讨论】:

  • 谢谢,但对我没有帮助。
  • 请看我的编辑。无论如何,如果您没有Android背景,很难解释一切。我建议您在进行编码之前至少浏览一些基本教程。
  • 是的,一切都如我所愿。非常感谢您的帮助:)我看书学习了,但是学习给我的例子更好(看完书我不知道如何解决你的问题。)
  • 没关系。我知道有些人喜欢边做边学。很高兴我能提供帮助。
【解决方案2】:

先为editTextValue设置findViewById,然后再设置getText()

【讨论】:

    【解决方案3】:

    这里有多个问题。第一个是您试图在尚未初始化的EditText 上调用getText()。但即使你有,你仍然会得到同样的错误,因为你还没有膨胀你的layout。在尝试访问layout 中的Views 之前,您需要先致电setContentViw()。此外,在onCreate() 运行之前,您不能尝试访问Views。它应该看起来更像以下内容...

    public class MainActivity extends Activity implements OnClickListener{
    
    Button btnGetInfo;
    TextView textView;
    EditText editTextValue;
    
    public String givenValue = ""; 
    
    public String URL = "http://example.com/script.php?value=";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        findViewsById();  // its fine to use this method but do it before trying to get the value from the EditText
        givenValue  = editTextValue.getText().toString();
        URL = URL.concat(givenValue);  // there are different ways but use one to concatenate the Strings
    

    这个方法 findViewsById() 应该可以工作,但它非常接近原生 Android 函数,所以我会考虑将其重命名为 init()loadViews() 或其他名称。

    【讨论】:

    • 参数没有传入url,很遗憾。
    • 你是否设置了断点来查看哪里问题出在哪里?这将解决您最初的问题。
    • 在初始化之前,您仍在尝试从 EditText 获取值。再看我的回答。
    • 是的,这是我的错(疏忽+缺乏知识)。但我更正了:) 谢谢你的帮助。你和 Entryleveldev 都说对了,我就是不能用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2017-08-31
    • 2018-11-13
    相关资源
    最近更新 更多