Android 如何将参数传递到URL并获得解析响应
如何将值传递到URL并解析响应 我的url是ASP.net “url/SomeProject/SomeApi/UserRequest/GetUserRequest/Id/userName” 在这里,我想将Id和用户名传递到url。然后,我会得到该特定id的响应。我需要解析响应并显示在我的活动中。许多其他选项之一Android 如何将参数传递到URL并获得解析响应,android,Android,如何将值传递到URL并解析响应 我的url是ASP.net “url/SomeProject/SomeApi/UserRequest/GetUserRequest/Id/userName” 在这里,我想将Id和用户名传递到url。然后,我会得到该特定id的响应。我需要解析响应并显示在我的活动中。许多其他选项之一 格式(“url/SomeProject/SomeApi/UserRequest/GetUserRequest/%s/%s”,id,用户名) 您可能希望在异步线程中调用这段代码,并在do
格式(“url/SomeProject/SomeApi/UserRequest/GetUserRequest/%s/%s”,id,用户名)
您可能希望在异步线程中调用这段代码,并在doInBackground()方法中使用
private class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... aurl) {
String urlParameters = null;
try {
urlParameters = "& Id =" + URLEncoder.encode("1", "UTF-8")+
"&GameId=" + URLEncoder.encode(Integer.toString(Mojoy.pref.getInt("gameCode", 0)), "UTF-8") +
"& userName =" + URLEncoder.encode(username, "UTF-8");
log.d("urlParameters", urlParameters);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String filename = “xyz”;
File file = new File(_context.getFilesDir(), filename);
try {
URL url = new URL(aurl[0]);
HttpURLConnection conexion = (HttpURLConnection) url.openConnection();
conexion.setConnectTimeout(5000);
conexion.setRequestMethod("POST");
conexion.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conexion.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
conexion.setRequestProperty("Content-Language", "en-US");
conexion.setUseCaches (false);
conexion.setDoInput(true);
conexion.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
conexion.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
InputStream input = conexion.getInputStream();
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return "true";
} catch (Exception e) {
return null;
}
}
}
希望这能有所帮助。经过一天的努力,终于找到了解决办法
String UserRequestId, LoginUserName, response;
try {
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet("http://url/VirtusMobile/VirtusApi/UserRequest/GetUserRequest/"+UserRequestId+"/"+LoginUserName);
HttpResponse httpResponse=httpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
}
现在可以解析此“响应”
感谢大家的支持:)请参阅和获取soap
String filename = getApplicationContext().getFilesDir()+"/xyz";
String UserRequestId, LoginUserName, response;
try {
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet("http://url/VirtusMobile/VirtusApi/UserRequest/GetUserRequest/"+UserRequestId+"/"+LoginUserName);
HttpResponse httpResponse=httpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
}