如何通过XML向服务器发送数据,并在android中获取响应

发布于 2024-11-19 19:09:04 字数 2579 浏览 2 评论 0原文

我正在使用 XML 文件向服务器发送一些请求。我应该得到相应的回应。但我在 URL 中遇到文件未找到异常。我的代码在这里。

public InputStream testPost(String xmlString,URL url)
{
    try
    {
        httpClient = new DefaultHttpClient();
        connection = (HttpURLConnection)url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/xml");
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(xmlString);
        outputStream.flush();
        isResponse = (InputStream) connection.getInputStream();
    }
    catch(UnknownHostException e)
    {
        Log.e("Failure","Network Failure"+e.toString());
    }       
    catch(SocketException e)
    {
        Log.e("Failure--","Network Failure"+e.toString());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return isResponse;
}



public void addLocation()
{
    try
    {
        String strAddLocation = "<useraccount>" +
        "<id>"+1+"</id>" +"<location>"+
        "<geocode>"+77.254188+","+28.692365+"</geocode>" +
        "<address>3523 16th street</address>" +
        "<city>Hyd</city>" +
        "<state>NY</state>" +
        "<zip>"+11006+"</zip>" +
        "<country>USA</country>" +
        "<is_primary>"+false+"</is_primary>" +
        "</location>"+"</useraccount>";

        Log.i("this", strAddLocation);

        urlPost = new UrlPost();

        addLocationInputStream= urlPost.testPost(strAddLocation, new URL(AppConstants.HOST_URL+AppConstants.ADD_LOCATION+1));
       if(addLocationInputStream!= null)
       {
           Log.i("the output is coming hare", "add location");
       }
         InputStreamReader reader = new InputStreamReader(addLocationInputStream );
         StringBuilder buf = new StringBuilder();
         char[] cbuf = new char[ 2048 ];
         int num;
         while ( -1 != (num=reader.read( cbuf )))
         {
             buf.append( cbuf, 0, num );
         }
         String result = buf.toString();
            System.err.println( "\nResponse from server after delete = " + result );
         reader.close();
            Log.i("reponce","\nResponse from server after delete = " + result);
    }

        catch(Exception ee)
        {
        Log.i("the error is here", ee.toString());
        }

}

I am Sending some request to server using XML file. And I should get a response accordingly. But i Am getting the File not found Exception in URL. My code is Here.

public InputStream testPost(String xmlString,URL url)
{
    try
    {
        httpClient = new DefaultHttpClient();
        connection = (HttpURLConnection)url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/xml");
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(xmlString);
        outputStream.flush();
        isResponse = (InputStream) connection.getInputStream();
    }
    catch(UnknownHostException e)
    {
        Log.e("Failure","Network Failure"+e.toString());
    }       
    catch(SocketException e)
    {
        Log.e("Failure--","Network Failure"+e.toString());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return isResponse;
}



public void addLocation()
{
    try
    {
        String strAddLocation = "<useraccount>" +
        "<id>"+1+"</id>" +"<location>"+
        "<geocode>"+77.254188+","+28.692365+"</geocode>" +
        "<address>3523 16th street</address>" +
        "<city>Hyd</city>" +
        "<state>NY</state>" +
        "<zip>"+11006+"</zip>" +
        "<country>USA</country>" +
        "<is_primary>"+false+"</is_primary>" +
        "</location>"+"</useraccount>";

        Log.i("this", strAddLocation);

        urlPost = new UrlPost();

        addLocationInputStream= urlPost.testPost(strAddLocation, new URL(AppConstants.HOST_URL+AppConstants.ADD_LOCATION+1));
       if(addLocationInputStream!= null)
       {
           Log.i("the output is coming hare", "add location");
       }
         InputStreamReader reader = new InputStreamReader(addLocationInputStream );
         StringBuilder buf = new StringBuilder();
         char[] cbuf = new char[ 2048 ];
         int num;
         while ( -1 != (num=reader.read( cbuf )))
         {
             buf.append( cbuf, 0, num );
         }
         String result = buf.toString();
            System.err.println( "\nResponse from server after delete = " + result );
         reader.close();
            Log.i("reponce","\nResponse from server after delete = " + result);
    }

        catch(Exception ee)
        {
        Log.i("the error is here", ee.toString());
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-11-26 19:09:04

您是否已将

<uses-permission android:name="android.permission.INTERNET" />

清单标记内部和应用程序标记外部添加到 AndroidMainfest.xml 文件中?

Have you added

<uses-permission android:name="android.permission.INTERNET" />

inside of manifest tags and outside of application tag to your AndroidMainfest.xml file?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文