如何将参数传递给URL并获得解析响应

时间:2014-05-29 10:53:25

标签: android

如何将值传递给URL并解析响应?

我的网址是ASP.net

“URL / SomeProject / SomeApi / UserRequest / GetUserRequest /标识/用户名”

在这里,我想将 ID userName 传递给网址。然后我得到对该特定身份的回复。我需要解析响应并显示在我的活动中。

3 个答案:

答案 0 :(得分:1)

最后在尝试了一天之后,找到了解决方案..

     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);


        }

这个"回应"现在可以解析..

感谢所有人的支持:)

答案 1 :(得分:0)

许多其他选项之一String.format()


String.format("url/SomeProject/SomeApi/UserRequest/GetUserRequest/%s/%s", id, userName)

答案 2 :(得分:0)

您可能希望在异步线程中调用此段代码并在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;
        }
     }
}

此代码将响应保存在文件xyz中,您可以根据需要进行解析和执行。

您可以像这样获取保存的文件路径:

String filename = getApplicationContext().getFilesDir()+"/xyz";

希望这有帮助。