【问题标题】:Get method issue in android在android中获取方法问题
【发布时间】:2013-02-07 05:57:58
【问题描述】:

我在 php 中创建了类似的 get 参数,并且我正在使用 Get 方法提交到链接。例如:“http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress=”。

如果用户输入edittext(比如aa@gmail.com)并提交,它将提交为

http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress=aa@gmail.com。怎么样

在安卓中可能吗?

我尝试通过一个例子来使用它。但它强制关闭

代码如下:

EditText Ename;
Button btncreate;
String n = null;
String contentOfMyInputStream1;
String output = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Ename = (EditText)findViewById(R.id.msgTextField);
    btncreate = (Button)findViewById(R.id.sendButton);
    btncreate.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {   
    String st1;
    st1 = Ename.getText().toString();
    try {
        output = "http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress="+st1;
        downloadUrl(output);//request been send
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
    if (output != null) {
        Toast.makeText(this, output, Toast.LENGTH_SHORT).show();
    }
}

public String downloadUrl(String url) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpRequestBase httpRequest = null;
    HttpResponse httpResponse = null;
    InputStream inputStream = null;
    String response = "";
    StringBuffer buffer = new StringBuffer();
    httpRequest = new HttpGet(url); 
    httpResponse = httpclient.execute(httpRequest);
    inputStream = httpResponse.getEntity().getContent();
    int contentLength = (int) httpResponse.getEntity().getContentLength();
    if (contentLength < 0)  {
        // Log.e(TAG, "The HTTP response is too long.");
    }
    byte[] data = new byte[256];
    int len = 0;
    while (-1 != (len = inputStream.read(data)) ) {
        buffer.append(new String(data, 0, len));
    }
    inputStream.close();
    response = buffer.toString();
    return response;
}

我收到错误

Fatal Exception:main
android.os.NetworkOnMainTreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:385)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)

请帮我解决问题。

提前致谢。

【问题讨论】:

    标签: android android-layout android-intent android-emulator


    【解决方案1】:

    就像Exception 告诉你的那样简单。您在主 Thread 上运行网络内容。使用AsyncTasknew Thread 来执行你的downloadUrl 方法。

    使用这个示例代码,取自here:

    private class LongOperation extends AsyncTask<String, Void, String> {
    
          @Override
          protected String doInBackground(String... params) {
                //Here you put your downloadUrl() method.
                //because this method does stuff in background
                return downloadUrl(params[0])
          }      
    
          @Override
          protected void onPostExecute(String output) {
              //after downloading is finished, toast it.
              if (output != null)
                  Toast.makeText(this, output, Toast.LENGTH_SHORT).show();
          }
    
          @Override
          protected void onPreExecute() {
          }
    
          @Override
          protected void onProgressUpdate(Void... values) {
          }
    }   
    

    通过这两行来调用它:

    LongOperation downloadTask = new LongOperation();
    downloadTask.execute(output);
    

    This site 可能也会有所帮助。

    【讨论】:

      【解决方案2】:

      Android 不允许您在主 (UI) 线程上执行耗时的任务(例如网络操作)。如果您需要发出 http 请求,则需要在单独的线程上创建它。

      要么使用 AsyncTask,要么在新线程中生成它。

      【讨论】:

        【解决方案3】:

        您不能在 Honeycombe 的 UI 线程上执行网络 IO。从技术上讲,它在早期版本的 Android 上是可能的,但这是一个非常糟糕的主意,因为它会导致您的应用停止响应,并可能导致操作系统因您的应用表现不佳而杀死您的应用。您需要运行后台进程或使用 AsyncTask 在后台线程上执行网络事务。

        Android 开发者网站上有一篇关于Painless Threading 的文章很好地介绍了这一点,并且将为您提供比此处实际提供的更深入的答案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-28
          • 2021-03-06
          • 1970-01-01
          • 2023-03-29
          • 2017-04-08
          • 1970-01-01
          • 1970-01-01
          • 2015-09-30
          相关资源
          最近更新 更多