0

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

提前致谢

4

2 回答 2

1

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

httpclient.wire.header httpclient.wire

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

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

以上不适用于所有情况。例如在 android 上,我使用客户端 ,为了激活 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
于 2012-05-17T17:45:40.643 回答
0

在您的 IDE(例如 Eclipse)中,在httpclient.execute(httppost);

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

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

于 2012-05-17T17:47:57.510 回答