我在 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)
请帮我解决这个问题。
提前致谢。
最佳答案
就像 Exception
告诉您的那样简单。您在主 Thread
上运行网络内容。使用 AsyncTask
或 new 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也可能有所帮助。
关于android - android中获取方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744594/