Android:将字符串上传到服务器

时间:2012-11-21 05:50:18

标签: java android web webserver httpclient

我正在尝试让我的用户能够报告我的Android应用程序自动捕获到我的服务器的小错误。它没什么大的,只是一个小文本框和发送按钮。

它应该发送3个字符串(错误,可选的用户描述和时间)到我的网站上专门用于捕获这些错误的文件。问题是,我完全不知道该怎么做。我只知道如何让我的应用程序从我的网站上读取信息,而不是相反。

我的网站上是否必须有特殊脚本? JSON字符串是否必要?我需要将字符串保存在那里。 (不是暂时的) 我是一个新手,所以任何帮助表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

您只需要通过http将值发布到服务器上的php脚本,该脚本将在文件或数据库中保存这些值。

答案 1 :(得分:0)

- 您的服务器上必须运行script,例如:php script

- 它实际上是在服务器上发布的网络服务,以便客户端可以访问它。

- 然后您需要对服务器执行HTTP Post,最好使用NameValuePair来发送数据。

这是我执行HTTP POST的代码:

public String postData(String url, String xmlQuery) {

        final String urlStr = url;
        final String xmlStr = xmlQuery;
        final StringBuilder sb = new StringBuilder();

        Thread t1 = new Thread(new Runnable() {

            public void run() {

                HttpClient httpclient = MySSLSocketFactory.getNewHttpClient();

                HttpPost httppost = new HttpPost(urlStr);

                try {

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            1);
                    nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpclient.execute(httppost);

                    Log.d("Vivek", response.toString());

                    HttpEntity entity = response.getEntity();
                    InputStream i = entity.getContent();

                    Log.d("Vivek", i.toString());
                    InputStreamReader isr = new InputStreamReader(i);

                    BufferedReader br = new BufferedReader(isr);

                    String s = null;

                    while ((s = br.readLine()) != null) {

                        Log.d("YumZing", s);
                        sb.append(s);
                    }

                    Log.d("Check Now", sb + "");

                } catch (ClientProtocolException e) {

                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } /*
                 * catch (ParserConfigurationException e) { // TODO
                 * Auto-generated catch block e.printStackTrace(); } catch
                 * (SAXException e) { // TODO Auto-generated catch block
                 * e.printStackTrace(); }
                 */
            }

        });

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Getting from Post Data Method " + sb.toString());

        return sb.toString();
    }

////////////////////////////编辑部分////////////// /////////////////////

服务器端php代码:

<?php

require_once(ROOT.'/lab/lib/xyz_api_int.php');

try {

    //setup the sdk
    $api = YumzingApiInt::_get(
        Config::get('api_int','url'),
        Config::get('api_int','key'),
        Config::get('api_int','secret')
    );

    //connect to the api
    $api->connect();

    //check our token
    echo $api->getToken();

} catch(Exception $e){
    sysError($e->getMessage());
}