【问题标题】:android testing HttpPost string for debugging purposesandroid测试HttpPost字符串以进行调试
【发布时间】:2012-05-22 14:16:45
【问题描述】:

我正在尝试使用 HttpPost 将数据从我的手机发送到 mysql,但我没有设置任何服务器,所以我想知道我是否可以看到我正在发送的确切内容以及它的外观。我对 JSON 以及它们的外观有点熟悉。所以我很好奇我会发送到 mysql 以进行调试。

public void sendDataDetail(View v){
    String name = nameText.getText().toString();

    if(name.length()>0){
        Log.w(TAG, name);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://site/post.php");
        Log.w(TAG, "After HttpPost");
        try{
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            Log.w(TAG, "After list");
            nameValuePairs.add(new BasicNameValuePair("location","Los Angeles"));
            Log.w(TAG, "After location");
            nameValuePairs.add(new BasicNameValuePair("item_name",name));
            Log.w(TAG, "After item");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.w(TAG, "After nameValuePairs");
            httpclient.execute(httppost);
            nameText.setText(""); // clear text box, but this should be back to front page
        }catch(ClientProtocolException e){
            Log.w(TAG, e.getMessage());
        }catch(IOException e ){
            Log.w(TAG, e.getMessage());

        }
    }
}

如您所见,我有一堆日志要测试它是否通过每一行(不确定这是否有必要 =\ )。我想知道我是否可以看到httppost 的样子,但如果我这样做似乎将无法正常工作

Log.w(TAG, httppost);

我得到了错误。

关于如何测试我发送到 mysql 进行调试的任何建议?

提前致谢

【问题讨论】:

    标签: java android debugging httpclient http-post


    【解决方案1】:

    根据您使用的 httpclient 版本,将有多种方式打开 2 个关键记录器:

    httpclient.wire.header httpclient.wire

    在一种类型的客户端中,您可以使用 java 系统属性来激活相应记录器上的 DEBUG 级别记录,这些记录器几乎总是由 HttpClient(标头和线路)使用...

     System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug");          System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    

    上述方法并非在所有情况下都有效。例如在 android 上,我使用 client 而且,为了激活 WIRE 和 HEADERS 的日志记录,我必须重新编译第 3 方包,更改 class= ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog (Log DEBUG ON | OFF)

    两个记录器都打开的 logcat 示例块可以完整地查看 http...

    I/PicasaAlbumService( 2218): Starting PHOTOService.onHandleIntent com.b2bpo.media.action.ALBUMLIST
    I/ActivityManager(  163): Displayed com.b2bpo.media/.ApplyGeoActivity: +1s349ms (total +5s944ms)
    D/dalvikvm( 2218): GC_CONCURRENT freed 292K, 44% free 3625K/6407K, external 387K/519K, paused 2ms+3ms
    D/class ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient( 2218): Attempt 1 to execute request
    D/class ch.boye.httpclientandroidlib.impl.conn.DefaultClientConnection( 2218): Sending request: GET /data/feed/api/user/default?fields=entry%2Ftitle%2Centry%2Fl
    ink%5B%40rel%3D%22http%3A%2F%2Fschemas.google.com%2Fg%2F2005%23feed%22%5D&start-index=1&max-results=10&alt=json HTTP/1.1
    D/ch.boye.httpclientandroidlib.wire( 2218): >> "GET /data/feed/api/user/default?fields=entry%2Ftitle%2Centry%2Flink%5B%40rel%3D%22http%3A%2F%2Fschemas.google.co
    m%2Fg%2F2005%23feed%22%5D&start-index=1&max-results=10&alt=json HTTP/1.1[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): >> "GData-Version: 2[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): >> "Host: picasaweb.google.com[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): >> "Connection: Keep-Alive[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): >> "Authorization: OAuth null[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): >> "[\r][\n]"
    D/ch.boye.httpclientandroidlib.headers( 2218): >> GET /data/feed/api/user/default?fields=entry%2Ftitle%2Centry%2Flink%5B%40rel%3D%22http%3A%2F%2Fschemas.google.
    com%2Fg%2F2005%23feed%22%5D&start-index=1&max-results=10&alt=json HTTP/1.1
    D/ch.boye.httpclientandroidlib.headers( 2218): >> GData-Version: 2
    D/ch.boye.httpclientandroidlib.headers( 2218): >> Host: picasaweb.google.com
    D/ch.boye.httpclientandroidlib.headers( 2218): >> Connection: Keep-Alive
    D/ch.boye.httpclientandroidlib.headers( 2218): >> Authorization: OAuth null
    D/ch.boye.httpclientandroidlib.wire( 2218): << "HTTP/1.1 403 Forbidden[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Expires: Fri, 17 Feb 2012 18:07:42 GMT[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Date: Fri, 17 Feb 2012 18:07:42 GMT[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Cache-Control: private, max-age=0, must-revalidate[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Content-Type: text/plain[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "X-Content-Type-Options: nosniff[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "X-Frame-Options: SAMEORIGIN[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "X-XSS-Protection: 1; mode=block[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Set-Cookie: S=photos_html=UJsnbriEAQZm6J0wEJjfgA; Domain=.google.com; Path=/; Secure; HttpOnly[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Server: GSE[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "Transfer-Encoding: chunked[\r][\n]"
    D/ch.boye.httpclientandroidlib.wire( 2218): << "[\r][\n]"
    D/class ch.boye.httpclientandroidlib.impl.conn.DefaultClientConnection( 2218): Receiving response: HTTP/1.1 403 Forbidden
    D/ch.boye.httpclientandroidlib.headers( 2218): << HTTP/1.1 403 Forbidden
    D/ch.boye.httpclientandroidlib.headers( 2218): << Expires: Fri, 17 Feb 2012 18:07:42 GMT
    D/ch.boye.httpclientandroidlib.headers( 2218): << Date: Fri, 17 Feb 2012 18:07:42 GMT
    D/ch.boye.httpclientandroidlib.headers( 2218): << Cache-Control: private, max-age=0, must-revalidate
    D/ch.boye.httpclientandroidlib.headers( 2218): << Content-Type: text/plain
    D/ch.boye.httpclientandroidlib.headers( 2218): << X-Content-Type-Options: nosniff
    D/ch.boye.httpclientandroidlib.headers( 2218): << X-Frame-Options: SAMEORIGIN
    D/ch.boye.httpclientandroidlib.headers( 2218): << X-XSS-Protection: 1; mode=block
    D/ch.boye.httpclientandroidlib.headers( 2218): << Set-Cookie: S=photos_html=UJsnbriEAQZm6J0wEJjfgA; Domain=.google.com; Path=/; Secure; HttpOnly
    D/ch.boye.httpclientandroidlib.headers( 2218): << Server: GSE
    D/ch.boye.httpclientandroidlib.headers( 2218): << Transfer-Encoding: chunked
    D/class ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient( 2218): Connection can be kept alive indefinitely
    

    【讨论】:

      【解决方案2】:

      在您的 IDE(例如 Eclipse)中,在 httpclient.execute(httppost); 处放置一个断点

      然后在“表达式”窗口中,添加httppost 变量,查看值是什么。

      现在你可以摆脱所有的日志记录了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-29
        • 2014-01-15
        • 1970-01-01
        相关资源
        最近更新 更多