我在 php 中创建了类似的 get 参数,并且我正在使用 Get 方法提交到链接。例如:“ http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress= ”。
如果用户输入edittext(比如aa@gmail.com)并提交,它将提交为
http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress=aa@gmail.com。怎么样
可能在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)
请帮我解决问题。
提前致谢。