我已经开发了一个ios应用,现在也将其实现为android应用。
我是android新手,URL格式有问题。
我通过以下代码在ios中输入uname和pwd信息:
NSString *checkForLogin = [NSString stringWithFormat:[[@"http://" stringByAppendingString:IP] stringByAppendingString: @"&what=LoginCheck.php&un=%@&pwd=%@" ],username.text,pwd.text];
对于android,我无法通过使用以下代码获得正确的最终URL:
nameValuePairs.add(new BasicNameValuePair("un", un.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pwd", pwd.getText().toString()));
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(constants.URL + "&what=LoginCheckJava.php" );
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF_8"));
最终网址应为:xxxxxxxx / loadhtml.php?where = DeliverySystem&what = LoginCheck.php&un = usertest&pwd = 1234567890
请帮助。谢谢
最佳答案
您可以通过以下方式调用此任务
new PostMethodTask().execute("xxxxxxxx/loadhtml.php");
这个任务对你来说很好
private class PostMethodTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response;
HttpPost httpPost = new HttpPost(params[0]);
String responseString = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("where", "DeliverySystem"));
nameValuePairs.add(new BasicNameValuePair("what", "LoginCheck.php"));
nameValuePairs.add(new BasicNameValuePair("un", "usertest"));
nameValuePairs.add(new BasicNameValuePair("pwd", "1234567890"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
response = httpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
return responseString;
} else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
关于android - 如何将输入传递到android中的url,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20172346/