android testing HttpPost string for debugging purp

2019-09-02 02:25发布

问题:

I'm trying to send data from my phone to mysql with HttpPost, but I don't have any server set up, so I was wondering if I can see what I'm sending exactly, and what it looks like. I'm a little familiar with JSON and the likes of how those look. So I'm curious as to what I'd be sending to mysql for debugging purposes.

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());

        }
    }
}

As you can see I have a bunch of Logs to test if it goes through each line (not sure if this is even necessary =\ ). I was wondering if I can see what the httppost looks like, but it seems like it won't work if I do something like this

Log.w(TAG, httppost);

I get errors with that.

Any suggestions as to how I can test what I'm sending out to mysql for debugging?

Thanks in advance

回答1:

Depending on the version of httpclient that you are using, there are going to be various ways of turning on 2 critical loggers:

httpclient.wire.header httpclient.wire

In one type of client, you can use java system properties to activate DEBUG level logging on the respective loggers almost always used by an 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");

above will not work in all situations. For example on android, i use the client and, in order to activate logging for WIRE and for HEADERS, i have to recompile the 3rd party package, changing class= ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog ( Log DEBUG ON | OFF )

sample block of logcat with both loggers turned on gives complete view of what's happening under the covers of 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:

in your IDE, such as Eclipse, put a breakpoint at httpclient.execute(httppost);

and then in the Expressions window, add the httppost variable, to see what the value is.

Now you can get rid of all that logging stuff