2013-02-07 25 views
1

我创建了像在PHP中获取参数而我正在使用Get方法提交到链接。例如:“http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress=”。在android中获取方法问题

如果用户的EditText类型(说[email protected])并提交,将提交作为

http://example.com/subscribe/?act=SubscribeForEmail&[email protected]。它怎么样

可能在android?

我尝试了这个例子。但是,强制关闭

的代码如下:

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) 

请帮我解决这个问题。

在此先感谢。

回答

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可能会有所帮助。

0

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

既可以使用AsyncTask,也可以在新线程中产生它。

0

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

在Android开发人员网站上有一篇关于Painless Threading的文章,对此进行了很好的介绍,并且会为您提供比实际提供的更好的深度。