android测试HttpPost字符串用于调试目的

时间:2012-05-15 05:12:48

标签: java android debugging httpclient http-post

我正在尝试使用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进行调试的任何建议?

提前致谢

2 个答案:

答案 0 :(得分:1)

根据您使用的httpclient版本,可以采用多种方式启用2个关键记录器:

httpclient.wire.header httpclient.wire

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

 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的日志记录,我必须重新编译第三方软件包,更改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

答案 1 :(得分:0)

在IDE中的

(例如Eclipse)在httpclient.execute(httppost);

处放置一个断点

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

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