2012-11-21 55 views
1

我想让我的用户能够报告我的android应用程序自动捕获到我的服务器的小错误。这不是什么大事,只是一个小文本框和发送按钮。Android:上传字符串到服务器

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

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

回答

0

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

-它实际上是一个Web服务在服务器上发布,以便客户端可以访问它。

-然后你需要做一个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()); 
} 
+0

谢谢!这是非常有用的!你介意给我看在服务器上运行的脚本吗? – Pkmmte

+0

@Pkmmte我已经添加了你所要求的服务器端脚本...请参阅编辑的部分 –

0

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

+0

我该如何做到这一点? – Pkmmte

+0

通过学习一些基本的PHP并理解它如何将数据发送到脚本。你可以通过调用一个看起来像“http://yourserver.org/saveerrrors.php?error=blabla&time=01011970(这将是一个获取请求,但一个职位将更适合这项工作)的网址发送它。 – galex

相关问题